If its a certain type of problem you could use the visitor pattern. But a simple virtual method, which maybe empty, may be used in some cases.
The visitor pattern works by having a visitor class, which implements different methods for different types:
Code:
class Visitor {
public:
virtual void visit(Car* car) = 0;
virtual void visit(Truck* truck) = 0;
virtual void visit(Motocycle* moto) = 0;
};
Then inside of the base class of objects you have an accept method which dispatches to the appropriate Visitor::visit method on its type:
Code:
class Basic_car {
public:
virtual void accept(Visitor* v) = 0; /* all derived classes must dispatch on type */
};
class Car : public Basic_car {
public:
void accept(Visitor* v) { v->visit(this); /* dispatch on Car */ }
};
class Truck : public Basic_car {
public:
void accept(Visitor* v) { v->visit(this); /* dispatch on Truck */ }
};
class Motocycle : public Basic_car {
public:
void accept(Visitor* v) { v->visit(this); /* dispatch on Motocycle */ }
};
There must also be derived visitors. You could have variants of this idea: the point is that each derived class calls the appropriate method from visitor, selecting the method based upon its own type.