Skip to content

Instantly share code, notes, and snippets.

@sdeming
Last active August 29, 2015 14:15
Show Gist options
  • Save sdeming/e7a32ca61414a52b2b10 to your computer and use it in GitHub Desktop.
Save sdeming/e7a32ca61414a52b2b10 to your computer and use it in GitHub Desktop.
diamond inheritance in c++...
#include <iostream>
using namespace std;
class A
{
public:
virtual void Hello() { cout << "Hello from A" << endl; }
virtual ~A() {}
};
class B : public A
{
public:
virtual void Eh() { cout << "Eh from B" << endl; }
virtual void Hello() { cout << "Hello from B" << endl; }
virtual ~B() {}
};
class C : public A
{
public:
virtual void Boo() { cout << "Boo! from C" << endl; }
virtual void Hello() { cout << "Hello from C" << endl; }
virtual ~C() {}
};
class D : public B, public C
{
public:
virtual B* get() { return static_cast<B*>(this); }
virtual ~D() {}
};
int main()
{
auto x = new D();
x->get()->Hello();
}
@sdeming
Copy link
Author

sdeming commented Feb 13, 2015

CPPFLAGS=--std=c++11 make a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment