# Classes as Members of Each Others

This topic is 5049 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I want two classes to be able to reference each other so I did this:
// A.h -----------------
class B;

class A
{
public:
B MemberBofA;
}

// B.h -----------------
class A;

class B
{
public:
A * PointerToA_from_B;
}

Then I got two errors, something like: using undefined struct/union/class... I know that I should have defined class B before A inorder to use B in A but in this case they should be able to use each... Is there a mechanism I don't know of that would help me?

##### Share on other sites
Just invert the order...

// B.h -----------------
class A;

class B
{
public:
A * PointerToA_from_B;
};

// A.h -----------------
class B;

class A
{
public:
B MemberBofA;
};

##### Share on other sites
What order...
Each of these is a different file...
They are included in the respective (A.Cpp or B.Cpp) files

##### Share on other sites
class A needs to know about all of B in order to use it as a member, so it should be:

// A.h -----------------
#include "B.h"

class A
{
public:
B MemberBofA;
}

// B.h -----------------
class A;

class B
{
public:
A * PointerToA_from_B;
}

##### Share on other sites
But why shouldn't B know all about A, and if I included it as you've done the Pointer in B still needs to know about A which is not defined until after that inclusion

##### Share on other sites
If a class has a member of a given type, then the size of that type must be known before the size of class itself is known. And since the size of a class must be known from its definition, you need to include the definition of the member type.

On the other hand, if the member is of type pointer to something, the size of the pointer is known (it is the same regardless of the type pointed to), so knowing the object isn't necessary.

The same argument is also true if you replace "size" by "default constructor".

##### Share on other sites
The problem is still there if it was a function that has a class as an argument as in here:

// A.h -----------------class B;class A{public:   B MemberBofA;}// B.h -----------------#include "A.h"class A;class B{public:   void f( A Argument );}

##### Share on other sites
In the function case you may want to change from parameter type by value to reference:

void f( const A& Argument );

This will work with a forward declaration too and it'll spare you an extra copy of the object.

##### Share on other sites
Quote:
 Original post by BrainCodingArts// A.h -----------------class B;class A{public: B MemberBofA;}

I haven't read the whole thread but this makes no sense, you need the definition of type B to be a member of type A.

Quote:
 Original post by BrainCodingArts// B.h -----------------#include "A.h"class A;class B{public: void f( A Argument );}

And in this case you only need presence of name of type A for function arguments use a constant/non-constant reference to A you don't need A's definition here.

A.hpp
//inclusion guard#ifndef _A_HPP_#define _A_HPP_ struct B;struct A {   void foo(const B&);   /* ... */};#endif

A.cpp
#include "A.hpp"#include "B.hpp"void A::foo(const B& b) { /*...*/ }/* ... */

B.hpp
//inclusion guard#ifndef _B_HPP_#define _B_HPP_struct A;struct B {   const A& ref_a; //or a pointer to A makes no difference   B(const A&);   /* ... */};#endif

B.cpp
#include "B.hpp"#include "A.hpp"B::B(const A& a): ref_a(a) {}/* ... */

[Edited by - snk_kid on October 24, 2004 5:05:54 AM]

1. 1
2. 2
JoeJ
20
3. 3
frob
19
4. 4
5. 5

• 10
• 10
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632199
• Total Posts
3004733

×