Using inherited classes in inherited classes (overloading)

Started by
2 comments, last by startreky498 18 years, 10 months ago
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.
Advertisement
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,
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?
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?

This topic is closed to new replies.

Advertisement