Skip to content

Instantly share code, notes, and snippets.

@sven-bock
Created December 14, 2018 10:25
Show Gist options
  • Save sven-bock/9f161759a8c91e068c1eb831fc82f5b5 to your computer and use it in GitHub Desktop.
Save sven-bock/9f161759a8c91e068c1eb831fc82f5b5 to your computer and use it in GitHub Desktop.
C++ Using base and derived constructor
struct Base {
Base(int a) : i(a) {}
int i;
};
struct Derived : Base {
Derived(int a, std::string s) : Base(a), m(s) {}
using Base::Base; // Inherit Base's constructors.
// Equivalent to:
//Derived(int a) : Base(a), m() {}
std::string m;
};
Derived now has two constructors (not counting copy/move constructors). One that takes an int and a string and one that takes just an int.
https://softwareengineering.stackexchange.com/questions/197893/why-are-constructors-not-inherited
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment