fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. namespace mylib {
  5. struct Person {
  6. static int counter;
  7. int id;
  8. Person() : id(++counter) {}
  9. void whoami() { cout << "I'm "<<id<<endl; }
  10. }; //A
  11. struct Friend: Person {}; //B -> A
  12. int Person::counter=0;
  13. }
  14. struct Employee : virtual mylib::Person {}; // C->A
  15. struct Colleague : Employee, mylib::Friend {}; // D->(B,c)
  16.  
  17. int main() {
  18. mylib::Friend p1; // ok !
  19. p1.whoami();
  20.  
  21. Employee p2;
  22. p2.whoami();
  23.  
  24. Colleague p3; // No diamond !
  25. //p3.whoami(); // ouch !! not allowed: no diamond so the function has to be called for which base object ?
  26. p3.Employee::whoami(); // first occurence of A
  27. p3.mylib::Friend::whoami(); // second second occurence of A
  28.  
  29. return 0;
  30. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
I'm 1
I'm 2
I'm 3
I'm 4