Last active
August 29, 2015 14:14
-
-
Save dirvine/e3cd176cc21cd581698f to your computer and use it in GitHub Desktop.
example inherit/crtp forwarding ctr for derived from base
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
#include <iostream> | |
#include <string> | |
using namespace std; | |
template <typename Child> | |
struct Routing { | |
Routing() = delete; | |
explicit Routing(std::string s) : t(s) {} | |
void Get() { static_cast<Child*>(this)->FacadeGet(); } | |
void Hello() { std::cerr << t; } | |
string t; | |
}; | |
template <typename Child> | |
struct MaidMgr { | |
void interface() { | |
static_cast<Child*>(this)->FacadeGet(); | |
CallHello(); | |
} | |
void CallHello() { static_cast<Child*>(this)->Hello(); } | |
void Get() { | |
std::cerr << "MaidMgr Get\n"; | |
static_cast<Child*>(this)->Hello(); | |
} | |
}; | |
template <typename Child> | |
struct DataMgr { | |
void interface() { | |
static_cast<Child*>(this)->FacadeGet(); | |
CallHello(); | |
} | |
void CallHello() { static_cast<Child*>(this)->Hello(); } | |
}; | |
struct Facade : public MaidMgr<Facade>, public DataMgr<Facade>, public Routing<Facade> { | |
Facade() = delete; | |
Facade(std::string s) : Routing(s) {} // forwarding ctr - unsure msvc will allow this | |
void FacadeGet() { cerr << "Derived implementation " << x << "\n"; } | |
string x = "test"; | |
}; | |
int main() { | |
Facade d(std::string("hello")); | |
d.template MaidMgr<Facade>::Get(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment