Jump to content
  • Advertisement
Sign in to follow this  
BrainCodingArts

Classes as Members of Each Others

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!