C++ class typecasting
-
Sorry about a non MFC question, but I thought we should have some good C++ Programmers here. I have a base class and a inherited class.
class base { base() Initialise(); }; class derived : public base { derived(); Initialise(); }
base *root;
Within my view class I want to initialiseroot
as either the base or derived class. I do this with the following code.switch(version) { case 0 : root = new base; break; case 1 : root = new derived; break; } root->Initialise();
This howerver always calls base::Initialise instead of the derived even for case 1. What can I do to call derived::Initialise :confused:? Of course I could create a pointer to derived and typecast chamber_root, but I dont want to do that. Also I could derive another class from base and use that instead of base. --- -
Sorry about a non MFC question, but I thought we should have some good C++ Programmers here. I have a base class and a inherited class.
class base { base() Initialise(); }; class derived : public base { derived(); Initialise(); }
base *root;
Within my view class I want to initialiseroot
as either the base or derived class. I do this with the following code.switch(version) { case 0 : root = new base; break; case 1 : root = new derived; break; } root->Initialise();
This howerver always calls base::Initialise instead of the derived even for case 1. What can I do to call derived::Initialise :confused:? Of course I could create a pointer to derived and typecast chamber_root, but I dont want to do that. Also I could derive another class from base and use that instead of base. ---Coremn wrote: What can I do to call derived::Initialise Declare
Initialise()
asvirtual
in the base class:virtual Initialise();
BTW, it's always a good idea to specify a return type for the method, even if it returns nothing. Not specifying it is non-portable and susceptible for misinterpretation by others.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Coremn wrote: What can I do to call derived::Initialise Declare
Initialise()
asvirtual
in the base class:virtual Initialise();
BTW, it's always a good idea to specify a return type for the method, even if it returns nothing. Not specifying it is non-portable and susceptible for misinterpretation by others.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"