• 13
• 18
• 19
• 27
• 10

# Forward Declarations and Derived Classes?

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

## Recommended Posts

Basically I have code similar to this:
#include "xyz.h"

class A : public xyz
{
...
};

Now I have another project that links to the lib containing the code above. However I don't want to have to include xyz.h in this project. Normally I can just use forward declarations in my header files, but I'm not sure in this case. Is it possible to do something like:
class xyz;

class A : public xyz
{
...
};

I don't believe I can, but I'm at work so I can't test this. Is there any easy way do remove the #include "xyz.h" line, or do I have to do some additional wrapping so that forward declarations will work? If you're interested in my scenario, I have an Engine library that uses several other libraries, for example Ogre. However I don't want my project, which has the Engine as a dependency, to need to have Ogre as a dependency.

##### Share on other sites
In order to derive an object from a class, the base class needs to be a complete type. Consider, instead of inheritance, using composition.

##### Share on other sites
I guess I would have to do something like:

SomeFile.cpp:

#include "SomeFile.h"#include "xyz.h"class someClass : public xyz{  ...};...

SomeFile.h:

// forward declarationclass someClass;class SomeWrapperClass{  ...  someClass* someClass;}

I'll try this out, was just seeing if there was some easy way other than wrapping it and implementing the class in a .cpp object. Guess any derived classes that use external libraries like Ogre will have to be treated in this manner.