How to use a constructor with two or more classes?

1

I'm developing an orientation-to-object library. I've created an iterator for my future container. I have advance_iterator and regress_iterator . Both give bidirectional_iterator . The code is:

#ifndef OBJSTD_ADVANCE_ITERATOR
#define OBJSTD_ADVANCE_ITERATOR
#include <cstddef>
namespace objstd
{
    template <typename data_type> class advance_iterator
    {
        public:
        advance_iterator(data_type* data_ref)
        {
            this->data_reference = data_ref;
        }
        advance_iterator(const advance_iterator& it) = default;
        advance_iterator(advance_iterator&& it) = default;
        virtual bool operator > (const advance_iterator& it) const
        {
            return this->data_reference > it.data_reference;
        }
        virtual bool operator < (const advance_iterator& it) const
        {
            return this->data_reference < it.data_reference;
        }
        bool operator == (const advance_iterator& it) const
        {
            return this->data_reference == it.data_reference;
        }
        bool operator != (const advance_iterator& it) const
        {
            return this->data_reference != it.data_reference;
        }
        virtual bool operator >= (const advance_iterator& it) const
        {
            return (*this).operator>(it) || (*this).operator==(it);
        }
        virtual bool operator <= (const     advance_iterator& it)const
        {
            return (*this).operator<(it) || (*this).operator==(it);
        }
        virtual advance_iterator& operator+=(const size_t skips)
        {
            this->data_reference += skips;
            return *this;
        }
        virtual advance_iterator& operator++()
        {
            *this += 1;
            return *this;
        }
        virtual advance_iterator& operator++(int)
        {
            advance_iterator& state = *this;
            this->operator++();
            return state;
        }
        virtual advance_iterator operator+ (const size_t skips)
        {
            return this->data_reference + skips;
        }
        //must be declared virtual for non-conservational
        virtual data_type& operator*() const
        {
            return *this->data_reference;
        }
        protected:
        data_type* data_reference;
    };
}

#endif // OBJSTD_ADVANCE_ITERATOR

regress_iterator

#ifndef OBJSTD_REGRESS_ITERATOR
#define OBJSTD_REGRESS_ITERATOR
#include <cstddef>
namespace objstd
{
    template <typename data_type> class regress_iterator
    {
        public:
        regress_iterator(data_type* data_ref)
        {
            this->data_reference = data_ref;
        }
        regress_iterator(const regress_iterator& it) = default;
        regress_iterator(regress_iterator&& it) = default;
        virtual bool operator > (const regress_iterator& it) const
        {
            return this->data_reference > it.data_reference;
        }
        virtual bool operator < (regress_iterator& it) const
        {
            return this->data_reference < it.data_reference;
        }
        bool operator == (regress_iterator& it) const
        {
            return this->data_reference == it.data_reference;
        }
        bool operator != (regress_iterator& it) const
        {
            return this->data_reference != it.data_reference;
        }
        virtual bool operator >= (regress_iterator& it) const
        {
            return (*this).operator>(it) || (*this).operator==(it);
        }
        virtual bool operator <= (regress_iterator& it)const
        {
            return (*this).operator<(it) || (*this).operator==(it);
        }
        virtual regress_iterator& operator-=(size_t backwards)
        {
            this->data_reference -= backwards;
            return *this;
        }
        virtual regress_iterator& operator--()
        {
            *this -= 1;
            return *this;
        }
        virtual regress_iterator& operator--(int)
        {
            regress_iterator& state = *this;
            this->operator--();
            return state;
        }
        virtual regress_iterator operator- (size_t backwards)
        {
            return this->data_reference - backwards;
        }
        //must be declared virtual for non-conservational
        virtual data_type& operator*() const
        {
            return *this->data_reference;
        }
        protected:
        data_type* data_reference;
    };
}

#endif // OBJSTD_REGRESS_ITERATOR

And bidirectional_iterator is defined as follows:

#ifndef OBJSTD_BIDIRECTIONAL_ITERATOR
#define OBJSTD_BIDIRECTIONAL_ITERATOR
#include "regress_iterator.hpp"
#include "advance_iterator.hpp"
namespace objstd
{
    template <typename data_type> class bidirectional_iterator : public advance_iterator <data_type>, public regress_iterator <data_type>
    {
    };
}

#endif // OBJSTD_BIDIRECTIONAL_ITERATOR

The problem is when creating an instance of type bidirectional_iterator(ponteiro) , because the compiler does not know how to boot. What am I doing wrong?

    
asked by anonymous 19.06.2014 / 04:04

1 answer

1

In C ++, constructors are not implicitly inherited by the derived class to avoid this kind of ambiguity. In C ++ 11 you can explicitly inherit one of the constructors using using , which would not make much sense in your case, since there are two bases.

The solution for you is to implement a constructor in your class:

template <typename data_type>
class bidirectional_iterator : public advance_iterator<data_type>,
                               public regress_iterator<data_type>
{
public:

    bidirectional_iterator(data_type* data_ref) :
      advance_iterator<data_type>(data_ref),
      regress_iterator<data_type>(data_ref) {
        // do nothing
    }

};
    
19.06.2014 / 04:13