D
danra
Hi,
I have a question which seems to me pretty basic, unfortunately I
can't seem to figure it out.
Let's say I have an abstract base class Vehicle and classes Car and
Truck which derive from it.
Let's say I have abstract base class A which contains a list of
vehicles, and I define a method to insert and handle a vehicle into
the list.
Then I want to define class B which contains a list of cars, and I
want to reuse A's method for handling a new vehicle.
Here's the code:
class A
{
public:
A();
virtual ~A() = 0;
protected:
//appends vehicle to m_vehicleList
virtual void InsertVehicle(Vehicle* vehicle);
Vehicle* m_vehicleList;
}
class B : public A
{
public:
B();
virtual ~B();
//appends car to m_vehicleList
virtual void InsertCar(Car* car);
}
B::InsertCar obviously makes use of A::InsertVehicle, which inserts
the car into m_vehicleList.
The problem is, B has a list of vehicles instead of a list of cars,
which would present a problem the moment I wanted to write a method
which makes use of the "Car-ness" of the cars in the list.
A possible solution seems dropping m_vehicleList and adding a
parameter to InsertVehicle:
class A
{
public:
A();
virtual ~A() = 0;
protected:
//appends vehicle to vehicleList
virtual void InsertVehicle(Vehicle* vehicle, Vehicle**
vehicleList);
}
class B : public A
{
public:
B();
virtual ~B();
//appends car to m_carList
virtual void InsertCar(Car* car);
protected:
Car* m_carList;
}
And in InsertCat invoke InsertVehicle(car,&m_carList). Unfortunately
this wouldn't work, and rightly so, since "a parking lot of cars
is-not-a parking lot of vehicles".
So the question is - how can I reuse code so I won't have to write a
seperate InsertCar, InsertTruck etc. for every derived class of A?
Thank you very much.
I have a question which seems to me pretty basic, unfortunately I
can't seem to figure it out.
Let's say I have an abstract base class Vehicle and classes Car and
Truck which derive from it.
Let's say I have abstract base class A which contains a list of
vehicles, and I define a method to insert and handle a vehicle into
the list.
Then I want to define class B which contains a list of cars, and I
want to reuse A's method for handling a new vehicle.
Here's the code:
class A
{
public:
A();
virtual ~A() = 0;
protected:
//appends vehicle to m_vehicleList
virtual void InsertVehicle(Vehicle* vehicle);
Vehicle* m_vehicleList;
}
class B : public A
{
public:
B();
virtual ~B();
//appends car to m_vehicleList
virtual void InsertCar(Car* car);
}
B::InsertCar obviously makes use of A::InsertVehicle, which inserts
the car into m_vehicleList.
The problem is, B has a list of vehicles instead of a list of cars,
which would present a problem the moment I wanted to write a method
which makes use of the "Car-ness" of the cars in the list.
A possible solution seems dropping m_vehicleList and adding a
parameter to InsertVehicle:
class A
{
public:
A();
virtual ~A() = 0;
protected:
//appends vehicle to vehicleList
virtual void InsertVehicle(Vehicle* vehicle, Vehicle**
vehicleList);
}
class B : public A
{
public:
B();
virtual ~B();
//appends car to m_carList
virtual void InsertCar(Car* car);
protected:
Car* m_carList;
}
And in InsertCat invoke InsertVehicle(car,&m_carList). Unfortunately
this wouldn't work, and rightly so, since "a parking lot of cars
is-not-a parking lot of vehicles".
So the question is - how can I reuse code so I won't have to write a
seperate InsertCar, InsertTruck etc. for every derived class of A?
Thank you very much.