By making a copy of an object, you sacrifice its polymorphic abilities. Whatever type of car you go through, the copy will be of type Car (base class), because that is what is declared as.
If you want to continue using polymorphism, use a pointer or link. Here is the version using the link:
class CarPrinter { public: CarPrinter(const Car& car); void printNumberOfDoors(); protected: const Car &car_;
As you can see, this way you can continue to use the constructor, which takes the link as an argument. (These links should not be const , although const makes sense as long as the CarPrinter target CarPrinter just printed.)
One potentially unwanted side effect is that you cannot change what the link refers to after creating the CarPrinter object. If you need to print information for another object, you need to create a new CarPrinter object for it. These objects would then really act as (probably short-lived) wrappers around links.
If you don't like this, you can still continue passing the reference to the constructor, but turn it into a pointer by taking its address in the constructor implementation, and then save it.
source share