Virtual Functions and Scope

Image

We can now understand why virtual functions must have the same parameter list in the base and derived classes (§15.3, p. 605). If the base and derived members took arguments that differed from one another, there would be no way to call the derived version through a reference or pointer to the base class. For example:

class Base {public:    virtual int fcn();};class D1 : public Base {public:    // hides fcn in the base; this fcn is not virtual    // D1 inherits the definition of Base::fcn()    int fcn(int);      // parameter list differs from fcn in Base    virtual void f2(); // new virtual function that does not exist ...

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.