Skip to content

Instantly share code, notes, and snippets.

@merryhime
Created July 21, 2019 18:31
Show Gist options
  • Save merryhime/5d3417019ccec2567ca9a7ddf0fe9060 to your computer and use it in GitHub Desktop.
Save merryhime/5d3417019ccec2567ca9a7ddf0fe9060 to your computer and use it in GitHub Desktop.
#include <cstdio>
class A {
public:
A() = default;
static void OverridableInit() {
std::printf("I am in A\n");
}
protected:
template <typename T>
A(T* this_) {
T::OverridableInit();
}
};
class B : public A {
public:
B() : A(this) {}
static void OverridableInit() {
std::printf("I am in B\n");
}
protected:
template <typename T>
B(T* this_) : A(this_) {}
};
class C : public B {
public:
C() : B(this) {}
static void OverridableInit() {
std::printf("I am in C\n");
}
protected:
template <typename T>
C(T* this_) : B(this_) {}
};
int main() {
C c{};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment