Sign in to follow this  

Using inherited classes in inherited classes (overloading)

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

Here's the setup: I have a virtual class "Graphics". I also have a virtual class "SPRITE". The following function exists, for an example: virtual void dialateImage(SPRITE *sourceImage, float sizeCoefficient)=0; Which would seem fine. The class that inherits Graphics is called "dx9". The previous function is redesigned to use a class that inherits the SPRITE class, which I called "d3dSprite": void dialateImage(d3dSprite *sourceImage, float sizeCoefficient); Maybe one can already see the problem here. When I try to compile, I get errors because I'm overloading when apparently I'm not allowed to with the code I've supplied: main.cpp(32) : error C2259: 'dx9' : cannot instantiate abstract class due to following members: 'void Graphics::dialateImage(SPRITE *,float)' : is abstract c:\Reality 101\C++ Game Engine\Text\01 Basic Text\Graphics.h(32) : see declaration of 'Graphics::dialateImage' 'bool Graphics::drawImage(SPRITE *,const RECT *,const POINT *)' : is abstract Now I can't very well change the new class's function member from "d3dSprite" to "SPRITE", since the new function uses variables not found in SPRITE. So how do I edit my code to allow for the function redefinition? Thank you.

Share this post


Link to post
Share on other sites
1) if you overload a virtual function by changing the parameter type, you are in fact creating a new function.

2) The easy solution: use dynamic_cast<>:


void dx9::dilateImage(SPRITE *sourceImage, float sizeCoefficient)
{
d3dsprite *aSprite = dynamic_cast<d3dsprite*>(sourceImage);
if (aSprite) // dynamic_cast<> returns NULL is the SPRITE is not a d3dsprite
{
// ...
}
}


You have to enable RTTI to achieve this.

The correct solution it to modify your design - because it obviously have some problems.

If you define both Graphic and SPRITE as abstract (we don't say virtual classes), you should limit yourself to the use of the interface of these classes. If you can't then there is a problem.

Regards,

Share this post


Link to post
Share on other sites
Thanks a lot!

But if I wish to avoid dynamic_cast, would i just change the Graphics class to something that's not an abstract class? For example, removing the "virtual" keywords and just working from that?

Share this post


Link to post
Share on other sites
I need to know the following:

Does this casting create a copy of the variable? I assume it probably does.

Now at the end of my code I need a similar function that will take a SPRITE, and then use that SPRITE to delete some variables in d3dSprite. Is there some code I can use to create a d3dSprite pointer to the SPRITE?

Share this post


Link to post
Share on other sites

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