Skip to content

Instantly share code, notes, and snippets.

@HappyCerberus
Created November 6, 2012 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HappyCerberus/4025319 to your computer and use it in GitHub Desktop.
Save HappyCerberus/4025319 to your computer and use it in GitHub Desktop.
Flat inheritance
#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();
}
#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