Can I have a class ("BIG") which has another class as a member (of type "Base"), so that upon construction, this "Base" member will actually be set to a derived class?
In particular, I want to have
class Base{
public:
void dostuff(){};
};
class DerivedA : public Base{
public:
void dostuff(){
//implementation version A
}
};
class DerivedB : public Base{
public:
void dostuff(){
//implementation version B
}
};
class BIG{
Base mything;
BIG(int Type){
if (Type==0)
mything=DerivedA();
if (Type==1)
mything=DerivedB();
}
};
Does C++ not allow this. In particular, I would be "Downcasting" mything from a Base to a DerivedA or a DerivedB. Am I correct that Downcasting is only allowed from Base pointers to Derived pointers, and only when the Base pointer is actually already a pointer to a Derived class?