J
Joseph Cook
I was wondering if there was any way to do the following:
If I have a class Base, and multiple Derived classes which all have
some different behavior on some function foo()...is there any good way
to have an object that can switch between these various
functionalities..using a pointer to the generic class?
as follows:
class Status
{
Status(){}
Base *mode;
Derived1 derived1;
Derived2 derived2;
};
class Base
{
virtual foo();
};
class Derived1 : public Base
{
foo(){cout<<"One";}
};
class Derived2 : public Base
{
foo(){cout<<"Two";}
};
Such that I can change *mode so that I can call mode->foo(), and can
expect to be able to choose whether mode->foo() is calling Derived1's
foo, or Derived2's foo. Is there any way to create such behavior?
Thanks!
If I have a class Base, and multiple Derived classes which all have
some different behavior on some function foo()...is there any good way
to have an object that can switch between these various
functionalities..using a pointer to the generic class?
as follows:
class Status
{
Status(){}
Base *mode;
Derived1 derived1;
Derived2 derived2;
};
class Base
{
virtual foo();
};
class Derived1 : public Base
{
foo(){cout<<"One";}
};
class Derived2 : public Base
{
foo(){cout<<"Two";}
};
Such that I can change *mode so that I can call mode->foo(), and can
expect to be able to choose whether mode->foo() is calling Derived1's
foo, or Derived2's foo. Is there any way to create such behavior?
Thanks!