avr39ripe

cppMultipleInheritanceProblemsAndSolutions

Aug 12th, 2021
972
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. class Super
  4. {
  5.     int superVal;
  6. public:
  7.     Super() : superVal{ 333 } { std::cout << "Super DEFAULT ctor\n"; }
  8.     Super(int superValP) : superVal{ superValP } { std::cout << "Super ctor superVal\n"; }
  9.     ~Super() { std::cout << "Super dtor\n"; }
  10.     void info() { std::cout << "Super info : "<< superVal << '\n'; }
  11. };
  12.  
  13. class BaseA: virtual public Super
  14. {
  15. public:
  16.     int valA;
  17. public:
  18.     BaseA() : Super{11},valA { 1 } { std::cout << "BaseA DEFAULT ctor\n"; }
  19.     BaseA(int valAP) : valA{valAP} { std::cout << "BaseA ctor valA\n"; }
  20.     ~BaseA() { std::cout << "BaseA dtor\n"; }
  21. };
  22.  
  23. class BaseB : virtual public Super
  24. {
  25. public:
  26.     int valB;
  27. public:
  28.     BaseB() :Super{ 22 },valB{2} { std::cout << "BaseB DEFAULT ctor\n"; }
  29.     BaseB(int valBP) : valB{ valBP } { std::cout << "BaseA ctor valB\n"; }
  30.     ~BaseB() { std::cout << "BaseB dtor\n"; }
  31. };
  32.  
  33.  
  34. class MultiChild : public BaseA, public BaseB
  35. {
  36. protected:
  37.     int childVal;
  38. public:
  39.     MultiChild() : Super{424242}, childVal { 3 } { std::cout << "MultiChild DEFAULT ctor\n"; }
  40.     MultiChild(int childValP) : Super{ 424242 }, childVal{ childValP } { std::cout << "BaseMultiChildA ctor childValP\n"; }
  41.     ~MultiChild() { std::cout << "MultiChild dtor\n"; }
  42. };
  43.  
  44. int main()
  45. {
  46.     MultiChild mChild{ 111 };
  47.    // mChild.valA;
  48.    mChild.BaseA::info();
  49.    mChild.BaseB::info();
  50.    mChild.info();
  51. }
  52.  
RAW Paste Data