Last active
October 1, 2016 08:05
-
-
Save caryhaynie/3099259 to your computer and use it in GitHub Desktop.
Simple CRTP pattern using friend classes to hide implementing methods.
This file contains hidden or 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> | |
| template <typename T> | |
| class TestBase { | |
| public: | |
| void TestIface() { | |
| static_cast<T*>(this)->TestImpl(); | |
| } | |
| }; | |
| #define IMPLEMENTS(impl, iface) friend class iface<impl> | |
| class Test : public TestBase<Test> { | |
| IMPLEMENTS(Test, TestBase); | |
| private: | |
| void TestImpl() { | |
| std::cout << "In TestImpl()" << std::endl; | |
| } | |
| }; | |
| template <typename T> | |
| void func(TestBase<T>* iface) { | |
| iface->TestIface(); | |
| } | |
| int main(int argc, char** argv) { | |
| Test t; | |
| t.TestIface(); | |
| func(&t); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have better solution w/o macro. See my gist: https://gist.github.com/pavelschon/67d987aaf173dfcc08279a132fccb9d8