Inheriting Constructors

In another move to simplify coding, C++11 provides a mechanism for derived classes to inherit constructors from the base class. C++98 already had a syntax for making functions from a namespace available:

namespace Box{    int fn(int) { ... }    int fn(double) { ... }    int fn(const char *) { ... }}using Box::fn;

This makes all the overloaded fn functions available. The same technique works for making nonspecial member functions of a base class available to a derived class. For example, consider the following code:

class C1{...public:...    int fn(int j) { ... }    double fn(double w) { ... }    void fn(const char * s) { ... }};class C2 : public C1{...public:...    using C1::fn;    double fn(double) { ... };};...C2 c2; ...

Get C++ Primer Plus 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.