Having trouble organizing my classes

Recommended Posts

Share on other sites
I don't have time to suggest you a better structure (this is not something you can just think of - it requires a pen, a notebook, etc), but I think I can help you with the technical details.

You only need to declare a class (lets call it CUsedClass) used by another class (lets call it CUserClass) before the CUserClass when CUserClass declares a variable of CUsedClass. When CUserClass declares a *reference* or a *pointer* to a variable of CUsedClass, the header *does not have to be included*, a single-line declaration is enough (class CUsedClass;). Then in cpp file for CUserClass, you actually include the h file for CUsedClass. Also, if CUserClass was derived from CUsedClass, then you would have to include CUsedClass's h file in the CUserClass's h file.

Let's put this in code:

Scenario 1 - declaring a variable (not a pointer or a reference)
//////////////////////////////////// UsedClass.h//////////////////////////////////#pragma onceclass CUsedClass{public:   CUsedClass();   ~CUsedClass();   // Other members follow...};//////////////////////////////////// UsedClass.cpp//////////////////////////////////#include "UsedClass.h"CUsedClass::CUsedClass(){}CUsedClass::~CUsedClass(){}// Other member's definitions follow//////////////////////////////////// UserClass.h//////////////////////////////////#pragma once// We are using a variable of CUsedClass, so we must include its header#include "UsedClass.h"class CUserClass{private:   CUsedClass m_usedClass;public:   CUserClass();   ~CUserClass();   // Other members follow};//////////////////////////////////// UserClass.cpp//////////////////////////////////#include "UserClass.h"CUserClass::CUserClass(){}CUserClass::~CUserClass(){}// Other member definitions follow, that do something to CUsedClass

It would be the same thing if CUserClass was derived from CUsedClass.

Scenario 2 - declaring a pointer to a variable

// UsedClass is unchanged//////////////////////////////////// UsedClass.h//////////////////////////////////#pragma onceclass CUsedClass{public:   CUsedClass();   ~CUsedClass();   // Other members follow...};//////////////////////////////////// UsedClass.cpp//////////////////////////////////#include "UsedClass.h"CUsedClass::CUsedClass(){}CUsedClass::~CUsedClass(){}// Other member's definitions follow//////////////////////////////////// UserClass.h//////////////////////////////////#pragma once// We are not including UsedClass.h, only declaring itclass CUsedClass;class CUserClass{private:   CUsedClass* m_pUsedClass; // pointer!public:   CUserClass();   ~CUserClass();   // Other members follow};//////////////////////////////////// UserClass.cpp//////////////////////////////////#include "UsedClass.h" // include UsedClass.h before the UserClass.h#include "UserClass.h"CUserClass::CUserClass(): m_pUsedClass(new CUsedClass){}CUserClass::~CUserClass(){}// Other member definitions follow, that do something to CUsedClass

[Edited by - ValMan on June 3, 2007 1:41:14 PM]

Share on other sites
Hi, and thanks for replying. :)

So you say that I could use a pointer declaration instead of a normal variable declaration to get around that problem, without even including the header? I love it.
I temporarily fixed the problem by moving my object manager to the game state class that needs it - but it's not a long term solution obviously. I'm definitely going for the solution you suggested now.

Thanks a lot for your help! :)

(Ps: Comments/suggestions on my class structure is of course still welcome... but I think if this works, my actual problem is solved. Thanks again. :))

Create an account

Register a new account

• Forum Statistics

• Total Topics
627719
• Total Posts
2978790

• 9
• 21
• 14
• 12
• 42