- no constructors or destructors are called, because no new objects are being created.
- Passing parameters by reference also avoids the slicing problem.
class Window{
public:
...
std::string name() const;
virtual void display() const;
};
class WindowWithScrollBar: public Window {
public:
...
virtual void display() const;
};
void printNmaeAndDisplay(Window w) { // parameter may be sliced!
std::cout << w.name();
w.display();
}
void printNameAndDisplay(const Window& w) { // fine, parameter won't be sliced
std::cout << w.name();
w.display();
}
References are typically implemented as pointers, so passing something by reference usually means really passing a pointer.