2

How can I create two classes that have member pointers to each other's class type, with full access to each other's data? In other words, I need two classes like this:

class A
{
protected:
   B *p;
};

class B
{
protected:
   A *p;
};

I'm having trouble with it because I'm not up to par with C++ conventions, and obviously, class A can't declare a class B because B is declared later in the code.

Thanks for the help

2
  • 2
    c++ is a place where friends have access to your private members. Had to mention it here :P
    – jrharshath
    Dec 16, 2010 at 12:40
  • 2
    Of course the question is: if you have two classes that are so heavily coupled together then are you sure your design is correct? Certainly sounds like you have a class boundary wrong somewhere.
    – GrahamS
    Dec 16, 2010 at 13:33

7 Answers 7

3

You should use forward class declaration.

//in A.h

    class B; // forward declaration
    class A
    {
    protected:
       B *p;
       friend class B; // forward declaration
    };

//in B.h
class A; // forward declaration
class B
{
protected:
   A *p;
   friend class A; // forward declaration
};
2
  • will it work if we remove the line "friend class A;" - so that B can not access "private member of A" back? is that line optional? :)
    – Crackie
    Mar 21 at 11:18
  • @Crackie It is not optional in the context of the original question which asks explicitly for total access: "How can I create two classes that have member pointers to each other's class type, with full access to each other's data?" - it is optional if you can do away with private members, like in a classic forward declaration Apr 3 at 7:01
2
class B;
class A {
    friend class B;
  protected:
    B *p;
};

class B {
    friend class A;
  protected:
    A *p;
};

Note that any member functions of A which actually use the members of B will have to be defined after the definition of B, for example:

class B;
class A {
    friend class B;
  protected:
    B *p;
    A *getBA();
};

class B {
    friend class A;
  protected:
    A *p;
};

A *A::getBA() { return p->p; }
1

you must use forward declaration like:

class B;
class A{
   ...
   friend class B;
};
1
  • Thanks... What about the friendship?
    – oldSkool
    Dec 16, 2010 at 12:23
1
class A
{
protected:
   class B *p;
};

If you want to declare friendship, you need a forward declaration:

class B;

class A
{
friend class B;
protected:
   B *p;
};
2
  • Not worth a downvote IMO, but you don't actually need a separate forward declaration in your second example. You could do friend class B and also class B *p, and leave out the first class B;. Dec 16, 2010 at 12:36
  • @Steve Yes, you are right. Don't know what I was thinking there. Dec 16, 2010 at 12:40
1
class B;
class A
{
protected:
   B *p;
   friend class B;
};

class B
{
protected:
   A *p;
   friend class A;
};
3
  • Will this code allow class A to use inline functions that were declared in class B?
    – oldSkool
    Dec 16, 2010 at 12:25
  • @old-school rules: inline is completely unrelated to whether functions are accessible or not. Dec 16, 2010 at 12:26
  • @Old-school rules: Yes, of course. Inline has nothing to do with friendship Dec 16, 2010 at 12:27
1

You could use a forward declaration by doing class B; above class A

1

simple use: class B;

class A
{
    protected:
       B *p;
       friend class B;
};

class B
{
    protected:
       A *p;
       friend class A;
};

Using class B; means a forward declaration and this basically tells the compiler: "class B exists somewhere".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.