Classes as Members of Each Others

Started by
7 comments, last by snk_kid 19 years, 6 months ago
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?
Advertisement
Just invert the order...

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

class B
{
public:
A * PointerToA_from_B;
};

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

class A
{
public:
B MemberBofA;
};

What order...
Each of these is a different file...
They are included in the respective (A.Cpp or B.Cpp) files
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;
}
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
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".
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 );}
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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]

This topic is closed to new replies.

Advertisement