Sign in to follow this  

Forward Declarations and Derived Classes?

This topic is 3629 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

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


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


Link to post
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 declaration
class 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.

Share this post


Link to post
Share on other sites

This topic is 3629 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this