Monday, July 31, 2017

C++11: Inherited Constructors

You can pull Base Class Constructors into a Derived Class’ scope. Here is an example:
#include <iostream>
class Base
{
 public:
  Base(int i) {std::cout << "Base One Parameter CTOR called.  ";}
};
class Derived : public Base
{
  using Base::Base; // Without this line, the program would not compile.
};
int main()
{
  Base    base(1)   ;
  Derived derived(1);

  return 0;
}
// Output: Base One Parameter CTOR called.  Base One Parameter CTOR called.
Reference: https://isocpp.org/wiki/faq/cpp11-language-classes#delegating-ctor

No comments:

Post a Comment