I have a base class Base, and few derived classes: Derived1, Derived2 and Derived3.
I would like to have Function funcOne in all of them so i could regularly access it like this:
Base* d1 = new Derived1();
Base* d2 = new Derived2();
Base* d3 = new Derived3();
d1->funcOne();
d2->funcOne();
d3->funcOne();
but i would like to have Function funcTwo ONLY in Derived1 class. The issue is, i want to access it like that:
d1->funcTwo();
Is it possible to do it in some way other than creating a virtual funcTwo in the base class with an implementation of some kind, for example
void funcTwo(){ cout << "There is no funcTwo for this class" << endl; }
And other implementation only for the Derived1 class?
Thanks!
Base
must be part ofBase
's definition. The best you can do is to have the function call compile successfully, but do nothing at runtime if it is not aDerived1
. – M.Md1
to be aDerived1 *
? i.e.Derived1* d1 = new Derived1()
instead of your current declaration. – davmac