Created
November 6, 2012 15:10
-
-
Save HappyCerberus/4025319 to your computer and use it in GitHub Desktop.
Flat inheritance
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> | |
using namespace std; | |
struct A | |
{ | |
int p_value; | |
void print() { cout << "Value = \"" << p_value << "\"." << endl; } | |
}; | |
template < typename T > | |
struct B | |
{ | |
void do_b(T& base) { base.p_value = 99; } | |
}; | |
template < typename T > | |
struct C | |
{ | |
void do_c(T& base) { base.p_value = 11; } | |
}; | |
template < typename A, template<typename> class B, template<typename> class C > | |
struct D : public A, protected B<A>, protected C<A> | |
{ | |
void do_b() { B<A>::do_b(*this); } | |
void do_c() { C<A>::do_c(*this); } | |
}; | |
int main() | |
{ | |
D<A,B,C> x; | |
x.do_b(); | |
x.print(); | |
x.do_c(); | |
x.print(); | |
} |
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> | |
using namespace std; | |
struct A | |
{ | |
int p_value; | |
void print() { cout << "Value = \"" << p_value << "\"." << endl; } | |
}; | |
struct B : virtual public A | |
{ | |
void do_b() { p_value = 99; } | |
}; | |
struct C : virtual public A | |
{ | |
void do_c() { p_value = 11; } | |
}; | |
struct D : public B, public C | |
{ | |
}; | |
int main() | |
{ | |
D x; | |
x.do_b(); | |
x.print(); | |
x.do_c(); | |
x.print(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment