A
Andrew Ward
Hi All,
Sorry if this is off topic, but I could not seem to find a suitable OO
Design newsgroup. If there is one feel free to let me know.
Here is a simplification of a general design problem I face.
Say you have:
class Car {
virtual void speedup();
virtual void slowdown();
};
class BondCar {
virtual void self_destruct();
};
Now storing a std::list<Car *> and calling speedup/slowdown is fine, however
if you wanted to self_destruct all BondCars in the list I can think of a few
ways to do it:
1) Use dynamic_cast, this is obviously not a good solution for more complex
designs.
2) Bring self_destruct down into the base class, making a 'fat' interface
3) Storing two lists, one std::list<Car *> and the other std::list<BondCar
*>, but then if I want to speed up all cars I have to iterate over two lists
None of these solutions seems very well suited to more complex designs, and
I have come across this problem over and over again. As it says in the c++
faq lite, inheritance is for old code to call new code, and NOT for code
reuse, so it almost seems to me that adding new public methods to derived
classes is wrong.
Could someone please enlighten me?
Andy
Sorry if this is off topic, but I could not seem to find a suitable OO
Design newsgroup. If there is one feel free to let me know.
Here is a simplification of a general design problem I face.
Say you have:
class Car {
virtual void speedup();
virtual void slowdown();
};
class BondCar {
virtual void self_destruct();
};
Now storing a std::list<Car *> and calling speedup/slowdown is fine, however
if you wanted to self_destruct all BondCars in the list I can think of a few
ways to do it:
1) Use dynamic_cast, this is obviously not a good solution for more complex
designs.
2) Bring self_destruct down into the base class, making a 'fat' interface
3) Storing two lists, one std::list<Car *> and the other std::list<BondCar
*>, but then if I want to speed up all cars I have to iterate over two lists
None of these solutions seems very well suited to more complex designs, and
I have come across this problem over and over again. As it says in the c++
faq lite, inheritance is for old code to call new code, and NOT for code
reuse, so it almost seems to me that adding new public methods to derived
classes is wrong.
Could someone please enlighten me?
Andy