2

I wrote an iterator class inside my HashSet class.

However, a few constructors of the iterator class will only be used inside the HashSet class.

I thought it was a good idea to hide them from the user by making them private. Is there a way to make inner class private methods accessible to outer class?

If not, is there another way to make those constructors only accessible from the inside of HashSet class?

Edit:

I know how to access outer class private members from inner class, but I am asking how to access inner class private members from outer class, so the question is not a duplicate.

This is my source code:

template <typename val_type, 
typename prehash = std::hash<val_type> >
class HashSet {

   ...

    template<typename val_type>
    class forward_iterator :
        public std::iterator<std::forward_iterator_tag, SList<val_type> > {

    public: // I want to keep those 3 constructors
                  // accessible to anyone

        forward_iterator() : 
            ptr(nullptr), 
            parent(nullptr),
            pos(-1) 
        {}

        forward_iterator(const forward_iterator* rhs) :
            ptr(rhs->ptr),
            parent(rhs->parent),
            ptr_it(rhs->ptr_it),
            pos(rhs->pos)
        {}
        forward_iterator(const forward_iterator& rhs) :
            ptr(rhs.ptr),
            parent(rhs.parent),
            ptr_it(rhs.ptr_it),
            pos(rhs.pos)
        {}

    private: // I want to hide the below constructors
             //from anyone not in  
             //HashSet class

        forward_iterator(pointer p, list_iterator liter, HashSet* prnt, 
        long long int pos_) : 
            ptr(p),
            ptr_it(liter),
            parent(prnt),
            pos(pos_)
        {}
        forward_iterator(reference p, list_iterator liter, HashSet* prnt, 
        long long int pos_) :
            ptr(p),
            ptr_it(liter),
            parent(prnt),
            pos(pos_)
        {}

        ...

     };

   ...

};
Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44
  • Possible duplicate of [Can inner classes access private variables?](https://stackoverflow.com/questions/486099/can-inner-classes-access-private-variables) – Onur A. Mar 03 '18 at 23:51
  • 4
    You can make the outer class a friend of the inner class. – Cris Luengo Mar 04 '18 at 02:25
  • "In C++, your friends can touch your privates." A joke to help you remember about that! – L.C. Nov 23 '18 at 21:01

0 Answers0