Hey,
I'm trying to use polymorphism as a type of generic/template, and it's exploding in my face bigtime.
I have this class:
It's an abstract class. It has a function in it called:
Code:
virtual classA GetClass() = 0;
classB inherits from classA with the following structure:
Code:
class classB : public classA
{
};
In it I'd like to have a function that returns an instance of classB, ex.
The caller will be thinking of it as an instance of classA, with all of the pure virtual functions implemented in classB.
There's a problem, if I change the return type to the child class (like above), it thinks I'm trying to override it, yells, fails to compile.
If I change the return type to classA (the parent), it realizes that this will instantiate a pure virtual / abstract class and pukes again.
I know that you can use polymorphism this way, I've used it before to call "down" into a child class (basically one object is going to be using classB objects as if they were classA objects, as there's a bunch of pure virtual functions in classA). Though I've never had to RETURN a child class as if it was a parent class.
As for the object using these classes, its prototype specifies classA, but it is passed classB, so it's not like I'm ever going to be instantiating pure virtuals.
Anybody know how I can get this done? I know it's do-able, never had to return it, though, and my design was kind of dependent on it
. Any help is greatly appreciated.
EDIT:
And obviously it goes without saying I can't change the prototype in classA to reflect the child class...