I've decided to recheck my whole application (3d engine) code structure, including finding the best of using headers, includes and forward declarations.
So far I used the following guidelines (100% used):
- headers don't include other headers
- if my class in a header has a function definition which needs an object of one of my other classes, I use a forward declaration. Example:
// d3d.h
#ifndef D3D_H
#define D3D_H
namespace MyEngine
{
class D3DScene;
class D3D
{
public:
void RenderScene(D3DScene *myScene)
..
...
}
This works for 90% of my code and structure.
Where I go wrong is in the following situation:
// d3d.h
#ifndef D3D_H
#define D3D_H
namespace MyEngine
{
class D3DFont;
class D3D
{
public:
D3DFont justafont; // this line produces the compiler error
...
void function1(D3DFont *fntpointer);
void function2(D3DFont &fntinstance);
void function3(D3DFont fntobject);
};
// d3dfont.h
class D3DFont
{
public:
// vars etc.
};
When I compile this, I get "D3D::justafont' uses undefined class 'MyEngine::D3DFont'
(not 100% the code, but pseudo good enough).
I studied some articles on forward declarations and understand that I can use them in my class header when:
- I pass through a object pointer in one of my member functions (like above; function1)
- I pass through an instance of an object in one of my member functions (like above; function2)
- It's not possible to pass through a complete object in a member function with forwar declaration (instead #include the header with the class definition) (likeabove; function 3)
In short;
- Can I have D3DFont justafont as member of D3D class, not being a pointer or var in a member function, using a forward declaration? (or do I have to include the header where D3DFont is declared?).
Maybe I already have an idea, because using D3DFont *myfonts; in the D3D class compiles without problems..
Only I don't see the logic in having a member variable (font object) declared as a pointer since it's not (as far as I understand).
Any help or advice is really appreciated.
I'm looking for the best way reduce includes in my D3D header in this case, and using forward declarations when possible.
Edited by cozzie, 11 January 2013 - 03:35 PM.






