Skip to content

Instantly share code, notes, and snippets.

@odeblic
Last active July 28, 2017 11:15
Show Gist options
  • Save odeblic/60ed17d13c442621db606c21e054a067 to your computer and use it in GitHub Desktop.
Save odeblic/60ed17d13c442621db606c21e054a067 to your computer and use it in GitHub Desktop.
Consequence of diamond inheritance
#include <iostream>
struct X
{
protected:
int i;
};
struct A : X
{
void action() {}
};
struct Av : virtual X
{
void action() {}
};
struct B : X
{
void action() {}
};
struct Bv : virtual X
{
void action() {}
};
struct C : A, B
{
void setAi(int j) { A::i = j; }
void setBi(int j) { B::i = j; }
int getAi() { return A::i; }
int getBi() { return B::i; }
void doActionA() { A::action(); }
void doActionB() { B::action(); }
};
struct V : Av, Bv
{
void setAi(int j) { Av::i = j; }
void setBi(int j) { Bv::i = j; }
int getAi() { return Av::i; }
int getBi() { return Bv::i; }
void doActionA() { Av::action(); }
void doActionB() { Bv::action(); }
};
int main(void)
{
C c;
c.setAi(1);
c.setBi(2);
std::cout << "c.getAi: " << c.getAi() << std::endl;
std::cout << "c.getBi: " << c.getBi() << std::endl;
c.doActionA();
c.doActionB();
V v;
v.setAi(1);
v.setBi(2);
std::cout << "v.getAi: " << v.getAi() << std::endl;
std::cout << "v.getBi: " << v.getBi() << std::endl;
v.doActionA();
v.doActionB();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment