Okay, so I have a class called CObject and there is a class called CMove that derives from it, and then a class called CMoveableObject that derives from CMove. In the CObject class there is a vector of pointers to CObject objects. Since CMoveableObject indirectly derives from CObject, the pointers should also work with CMoveableObject objects. In my program I have a function that isn't inherited, and only exists in the CMoveableObject class. I need to be able to call this function from the vector in the CObject class. Here is the specific line:
objects[0] is a pointer to a CMoveableObject object (again, in which the handleMovement function is defined), but I'm getting a "error C2039: 'handleMovement' : is not a member of 'CObject'". How can I "tell" the compiler that the pointer should still be able to work, or how can I work around this problem? Thank you!
1_ Make those methods in the parent class. They could have a default behaviour.
A_ Perform a cast. You could have a method that returns the type.
For more information, look for the composite pattern.
Sorry for the late reply, and thank you! I went with performing a cast. Perhaps not the most elegant solution, but it works, at least for now ;) Thanks!
I think there are some problems with Bernie's reasoning. The example about the ball doesn't use inheritance correctly. A bouncing ball shouldn't inherit from a ball, it has-a relationship with a ball, it's a verb, it can't -be- a ball. Would be kind of like saying 'Rolling is-a wheel'. Of course inheritance can cause all sorts of problems if you use it like this.
It makes much more sense to derive wheel from rolling. Of course, that's the Java way, and you get cluttered classes like that. Making *-able classes and deriving from them is a crazily over-complicated way to do things, and causes lots of confusion and problems. Inheritance should only be used when very necessary.
For me, a Ball might inherit from something like a sphere, and a basketball from a ball. I don't see any need to inherit more than that.
The Sphere class would handle the geometery; the Ball class would handle traits such as physical properties like weight, bounce, hollowness, friction, air pressure, material perhaps; and the Basket Ball class would configure the traits derived from the above two classes so you have an object that resembles all the traits of a basic basketball. From there you could change various properties for that specific basket ball object, such as a lower air pressure which would affect bounce, a different material (synthetic or leather, for example) which could affect friction, and so-on.
From the description of the problem, two simple solutions come to mind:
1. dynamic_cast to derived pointer. Just make sure the cast succeeds and it is of that type.
2. Apply a strategy similar to the Null Object pattern. This involves adding an empty virtual method to the base class and overriding its implementation in the derived classes such that you can always call it--but it only does what you want with the proper derived objects.
I'm sure there are other solutions but they're not going to be this simple.
Thank you. My hierarchy is essentially CObject -> CMove -> CMoveableObject and CMove is the only abstract class. I want to be able to instantiate CObject's and if I need to be able to move them then I would create a CMoveableObject, so in this situation I just went with using cast's.