How to use a constructor with two or more classes?

Asked

Viewed 111 times

1

I am developing an object-oriented library. I created an iterator for my future container. I have advance_iterator and regress_iterator. Both give rise to 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 such:

#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 creating an instance like bidirectional_iterator(ponteiro), because the compiler doesn’t know how to boot. What I’m doing wrong?

1 answer

1


In C++ constructors are not implicitly inherited by the derived class precisely to avoid this kind of ambiguity. In C++11 you can explicitly inherit one of the constructors using using, that 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
    }

};
  • I’ve already tried to do that, but I got the bug that there are no such members in the class.

  • 1

    @Lucashenrique Opa, missed the template argument there at startup. Fixed.

  • Now I understand; thank you. I’ll check after I sleep tired^.

  • William, some mistakes appeared to me due to ambiguity and I don’t know what to do. You can help me?

  • @Lucashenrique, are you not having some compiler problem? I tested William’s code is here and I made no mistake...

  • @Mass he updated before your comment.

  • So I think you can mark his answer as the correct one! D

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.