Skip to main content
gamedev.net
🔒 Locked

Application design, class knowledge

Started by _buu_ 5 replies 350+ views
Original Post
_buu_
_buu_
I have two classes, class Frame and class Button. Class Frame contains Buttons so naturally it needs to see the declaration for Button, so it can call methods on button objects and so on. However, the Button object needs to call functions of the Frame, specifically for the ''click'' function, so that it can update state stored in the frame, so of course Button needs to see Frames declaration. But I can''t do both! I have to include one then include the second one so they both can''t know about each others methods. I''m stumped on how to get out of this, aside from perhaps an ugly hack involving a global.
Thorin_nz
Thorin_nz
If your functions arent inline and are implemented in separete cpp files this shouldnt be an issue unless one of your class contains an instance of the other, In which case you can just swap the include order or have a pointer and allocate / deallocate memory for it in the constructor.

----------------

If you have a pointer you will only be able to return the pointer and not actually call functions***

[edited by - thorin_nz on January 1, 2004 5:34:33 AM]
George2
George2
It''s called a forward declaration :



A.h :

class B; // forward declaration,


class A
{
B* bar;
/* You can only use pointer or reference. The compiler knows
that B exists but doesn''t know what it is, and thus it can''t
know it''s size, so it has no way of knowing how big A is
if you would have done this :
B bar;
*/


};

A.cpp :

#include "A.h"
#include "B.h"

...

B.h :

#include "A.h"

class B
{
A* foo;
};


"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
_buu_
_buu_
quote:
Original post by George2
It''s called a forward declaration :



A.h :

class B; // forward declaration,


class A
{
B* bar;
/* You can only use pointer or reference. The compiler knows
that B exists but doesn''t know what it is, and thus it can''t
know it''s size, so it has no way of knowing how big A is
if you would have done this :
B bar;
*/


};

A.cpp :

#include "A.h"
#include "B.h"

...

B.h :

#include "A.h"

class B
{
A* foo;
};





I tried playing around with something like that, but the problem is that both classes need to call each others methods

Sneftel
Sneftel
You can do this with no problems, as long as you separate your class member function definitions from your class definition (the former goes in .cpp files, the latter in .h files). For more info, check out the article on source code organization here.


"Sneftel is correct, if rather vulgar." --Flarelocke
_buu_
_buu_
quote:
Original post by Sneftel
You can do this with no problems, as long as you separate your class member function definitions from your class definition (the former goes in .cpp files, the latter in .h files). For more info, check out the article on source code organization here.


"Sneftel is correct, if rather vulgar." --Flarelocke


Ah, after much re-reading this solved my problem perfectly. Much thanks.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.