Hi,
I have an array of base class pointers which I initialize with derived class pointers. However when I call the Run() function, only the base class one gets called.
Heres my code -
Code:
void SomeFunction()
{
Car** carArray = (Car**)new SportsCar*[100];
for(int i = 0; i < 100; ++i)
carArray [i] = new SportsCar();
CreateCars(carArray ,100);
}
void CreateCars(Car** carArray, int num)
{
Car** car = carArray;
for(int i = 0; i < num; ++i)
{
car [i]->SetSpeed(rand()%100);
}
carDriver = new carDriver(car,num);
}
class Car
{
...
virtual void Run() { cout << "CAR" << endl; }
};
class SportsCar
:public Car
{
void Run() { cout << "SPORTS CAR" << endl; }
};
All I get is a "CAR", any Ideas ? also, my Car class has a few pointers..do I need to define a copy constructor/assignment operator based on the code I posted above ?