Skip to content

Instantly share code, notes, and snippets.

@overminder
Created November 1, 2017 17:15
Show Gist options
  • Save overminder/0b9e4ff25258a2be46c5804a059822b6 to your computer and use it in GitHub Desktop.
Save overminder/0b9e4ff25258a2be46c5804a059822b6 to your computer and use it in GitHub Desktop.
Inheriting C++ base class constructor
// 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