Ralph Gaston
Ralph Gaston

Reputation: 43

Passing pointer to self

C++ I have a window class, parent, that creates several tab windows, child1, child2, ect.. The parent window has member variables for each child window. I want to access child2 from child1 and thought I might be able to pass a pointer to parent in the constructor of child1 and use parent's pointer to access child2. I don't even really know how to get started. I have tried something like the following and it doesn't compile, starting with the line containing (*this).

class CParent
{
public:
    CParent() {};
    CChild1 m_Child1(*this);
    CChild2 m_Child2;
    ....
};

class CChild1
{
public:
    CChild1(CParent *pParent) {};
    int getChild2Number(return pParent->m_Child2.m_number);
};

class CChild2
{
public:
    CChild2() {};
    m_number = 1;
}

I was wondering if anyone could help out with 1. Getting this to compile without changing up the structure too much 2. Advice on a better way to set this relationship up.

Upvotes: 1

Views: 3637

Answers (2)

Adam H. Peterson
Adam H. Peterson

Reputation: 4601

Use the initializer list for the children and pass this. However, be aware that some uses of this are incorrect inside an initializer list (since the object is still under construction and some members will not be initialized yet).

If m_Child1 needs to use m_Child2, you should put m_Child2 above m_Child1 in the class definition so it will be initialized first. Then you can pass the this pointer to m_Child2 like this:

struct CParent {
    CParent() : m_Child1( this ) { }
private:
    CChild2 m_Child2;
    CChild1 m_Child1;
};

You might consider passing just m_Child2 to m_Child1, though, to make the data dependency explicit, to give a hint why m_Child2 must come first, and to not provide a reference to an object under construction. That would look like this:

struct CChild2;  // Forward declare the class so CChild1 can accept it
struct CChild1 { // Define CChild1 first because CParent uses it for a member
    CChild1( CChild2 &child2 );
    // ...
};
// ...
struct CParent {
    CParent() : m_Child1( m_Child2 ) { }
private:
    CChild2 m_Child2;
    CChild1 m_Child1;
};

Upvotes: 4

Pixelchemist
Pixelchemist

Reputation: 24956

Your code has several severe issues concerning declaration and definition syntax, member initialization etc. My advise is to keep going with C++ basics.

I suggest to use a dynamic storage for your children since your questions gives a hint that there may be more than two of them.

class CParent;

class CChild
{
public:
  CChild (CParent * p_parent);
  int get_other_child_number(size_t const other_child);
  int number (void) { return m_number; }
private:
  CParent * m_parent;
  int m_number;
};

class CParent
{
public:
  CParent (void) { }
  void add_child (void) { m_children.emplace_back(this); }
  CChild & child (size_t const child_num) { return m_children.at(child_num); }
  CChild const & child (size_t const child_num) const { return m_children.at(child_num); }
  size_t num_childs (void) const { return m_children.size(); }
private:
  std::vector<CChild> m_children;
};

CChild::CChild (CParent * p_parent) : m_parent(p_parent), m_number(p_parent->num_childs()) { }
int CChild::get_other_child_number(size_t const other_child)
{ 
  return m_parent->child(other_child).number(); 
}


int main()
{
  CParent par;
  par.add_child();
  par.add_child();
  cout << "Number of Child 0 is " << par.child(0).number() << endl;
  cout << "Number of Child 1 is " << par.child(1).number() << endl;
  cout << "Number of Child 0 from Child 1 is " << par.child(1).get_other_child_number(0) << endl;
  cout << "Number of Child 1 from Child 0 is " << par.child(0).get_other_child_number(1) << endl;
}

Upvotes: 1

Related Questions