Created
December 14, 2018 10:25
-
-
Save sven-bock/9f161759a8c91e068c1eb831fc82f5b5 to your computer and use it in GitHub Desktop.
C++ Using base and derived constructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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