I have base class (virtual), with some children, and an array with pointers on base classes. When I create new object and then add its adress to the array, its ok. But when instead of adding adress I use new class(), the PARENT function is called, not the CHILD. Example code:
class base
{
public:
virtual int foo() {return 1;};
};
class child : public base
{
int foo() {return 2;};
};
//somewhere in code...
vector<base*> arr;
//1. Its ok:
child one;
arr.push_back(&one);
cout<<arr[0].foo(); //Its 2
//2. Its not ok...
arr.push_back(new child())
cout<<arr[1].foo(); //Its 1...
Thanx for ay help…
EDIT:
Im sorry, it was compiler fault – Downloaded MinGW and works fine… Sorry for problem…
foo is not public in child
also while I am here it should be
so
result