-1

I have a Container-Class and inside a Pointer-Class. The Container-Class has a private variable (myTreeSize), which I need also for the Pointer-Class. I tried to use reference or friend, but it won't work. Maybe someone has an idea?

template <typename Key>
class myContainer{
class myPointer;
using littlePoint = myPointer;
private:
  struct node{ 
    //....
  };
  struct mylist{  
    //....
  };

  mylist* tree{nullptr};
  size_type myTreeSize{0};

public:
  littlePoint find(int key) {
   mylist *list_pos{find_list(key)}; //return list-position
    node *node_pos {find_node(key)}; //return node-position
   if (list_pos && node_pos) return littlePoint{list_pos, node_pos};
   return end();
}

//.....methods
};

template <typename Key>
class myContainer<Key>::myPointer{
  mylist *list_pos;
  node *node_pos;

  myContainer& parent; //<--- no private variables
  friend class myContainer; //<-- no effect

  explicit myPointer(mylist *list_pos=nullptr, node *node_pos = nullptr):
     list_pos{list_pos}, node_pos{node_pos} {
      //...
}
}
rustyx
  • 80,671
  • 25
  • 200
  • 267
AO_30
  • 39
  • 5
  • 1
    What kind of "work" won't it do? – Drew Dormann Jun 21 '18 at 18:56
  • the access to the private variable mySize won't work, only public methods – AO_30 Jun 21 '18 at 18:59
  • getter and setter functions? After all, isn't the point of private that it's private? – John Perry Jun 21 '18 at 18:59
  • `myPointer` isn't declared anywhere. – François Andrieux Jun 21 '18 at 19:01
  • The `friend` has to be placed in the class that wants to grant access to it's private mebers to another class/function. – t.niese Jun 21 '18 at 19:01
  • if it´s private, you cannot access to it externally, that's the point....Declare it public and use other kinds of variable protection. – Capie Jun 21 '18 at 19:02
  • yes, but I thought it's maybe possible for nested classes, so there is no option to use the private variable? Only public? – AO_30 Jun 21 '18 at 19:02
  • 2
    If I declare `myPointer` in `myContainer` the code doesn't produce any error. Please supply a [mcve]. – François Andrieux Jun 21 '18 at 19:02
  • Possible duplicate of [Nested class' access to enclosing class' private data members](https://stackoverflow.com/questions/1604853/nested-class-access-to-enclosing-class-private-data-members) – Acorn Jun 21 '18 at 19:11
  • it's not always easy to explain the problem in english, I thought that I described my problem well, I din't expect that the word "work" would cause problems But thank you for your help, I get some ideas for my code to try – AO_30 Jun 21 '18 at 19:36

2 Answers2

1

myContainer& parent must be initialized. References can't be left uninitialized. If I fix that then your code compiles, and the friend declaration isn't needed as inner class has full access to the outer class already.

#include <iostream>
#include <string>

using namespace std;

template <typename Key>
class myContainer {
    class myPointer;
private:
    struct node {
        //....
    };
    struct mylist {
        //....
    };

    mylist* tree{ nullptr };
    int myTreeSize{ 0 };

public:
};

template <typename Key>
class myContainer<Key>::myPointer {
    mylist *list_pos;
    node *node_pos;

    myContainer& parent;

    explicit myPointer(myContainer& parent, mylist *list_pos = nullptr, node *node_pos = nullptr) :
        parent(parent), list_pos{ list_pos }, node_pos{ node_pos } {
        parent.myTreeSize; // no problem!
    }
};

int main()
{
    myContainer<int> c;
}

DEMO

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • thank you for the effort, so if I want to use myPointer as a return-Statement (in myPointer find) I need to extent the code – AO_30 Jun 21 '18 at 19:46
  • Yes `return myPointer{*this, list_pos, node_pos};` – rustyx Jun 21 '18 at 19:52
  • I extend the constructor and tried the new return it in my find function, but unfortunately I get errors ("no matching function for call...") – AO_30 Jun 21 '18 at 20:08
0

There is no myPointer shown in the outer class. Declaring nested classes does not automatically create any instance of that class. In the same vein, there is no "outer" instance for the "inner" class either.

A trivial example that actually instantiates the inner class and passes a reference to the parent to it:

template <typename T>
class A
{
    class B;
    B b_;

public:
    A() : b_(*this) {}
};

template <typename T>
class A<T>::B
{
    A & a_;

public:
    B(A & a) : a_(a) {}
};
Acorn
  • 24,970
  • 5
  • 40
  • 69