Created
July 22, 2013 21:48
-
-
Save metagoto/6058024 to your computer and use it in GitHub Desktop.
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
/* | |
// for this particular class, we don't want to do that: | |
struct A { | |
//... ctors etc | |
// (long) series of useful methods accessing private members | |
int method1(); | |
int method2(); | |
int method3(); | |
private: | |
int i, j; | |
}; | |
A a; | |
a.method1(); | |
a.method2(); | |
*/ | |
// ### the new design ### | |
// will act as a "friend namespace" | |
template<class> struct internal; | |
// the useful class | |
struct A { | |
//... ctors etc | |
// no more public methods | |
private: | |
int i, j; | |
// we add one single friend struct | |
template<class> friend struct internal; | |
}; | |
// implementation outside the class | |
// declare one tag | |
struct task1 {}; | |
// a bunch of "methods" | |
template<> | |
struct internal<task1> { | |
static int func1(A& a) { /* full access to A's private members */ } | |
static int func2(A& a) { /* ditto */ } | |
}; | |
// yet another tag and associated implementation | |
struct task2 {}; | |
template<> | |
struct internal<task2> { | |
static int func1(A&) {} | |
}; | |
// API now consists of free functions | |
int func1(A& a) { return internal<task1>::func1(a); } | |
int func2(A& a) { return internal<task1>::func2(a); } | |
int funcX(A& a) { /* can potentially tag-dispatch on task1 or task2 */ } | |
int main() { | |
A a; | |
// free functions FTW | |
func1(a); | |
func2(a); | |
funcX(a); | |
// say we want more. alright! | |
// #include "task3.hpp" | |
// funcZ(a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment