Created
November 1, 2017 17:15
-
-
Save overminder/0b9e4ff25258a2be46c5804a059822b6 to your computer and use it in GitHub Desktop.
Inheriting C++ base class 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
// Provides a mechanism to `inherit` a base class constructor | |
// verbatim (i.e., with the exact same parameter list) in a subclass. | |
// | |
// Note that in C++11 and onwards there's a better way to do it: | |
// See https://stackoverflow.com/questions/8093882/using-c-base-class-constructors | |
class Foo { | |
public: | |
#define FOO_CTOR(name, body, ...) \ | |
explicit name(int x, int y) \ | |
: __VA_ARGS__ \ | |
body | |
FOO_CTOR(Foo, {}, x_(x), y_(y)) | |
#define INHERIT_FOO_CTOR(name) \ | |
FOO_CTOR(name, {}, Foo(x, y)) | |
private: | |
int x_, y_; | |
}; | |
class Bar: public Foo { | |
public: | |
INHERIT_FOO_CTOR(Bar) | |
Bar *mkBar() { | |
return new Bar(1, 2); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment