How can i get my Base pointer to instantiate a derived object and use its functions? Is there a way to type cast this like java? Consider this sample code:
int main(){
cBase *pBase = 0;
if (1 < 2)
{
pBase = new cDerived;
}
}
class cBase
{public:
virtual void printMe()
{
std::cout << "printed cBase" << endl;
}
};
class cDerived: public cBase
{public:
virtual void printMe()
{
std:: cout << "printed cDerived" << endl;
}
};
However when i do this; it gives me an error "Expression must have class type".
cBase *pBase = 0;
if (1 < 2)
{
pBase = new cDerived;
pBase.printMe();
}
pBase.printMe();===>pBase->printMe();, andprintMe()had better be a member (virtual or otherwise) ofcBase.cBasewith a virtual functionprintMe?