User Tools

Site Tools


cpp:virtual_inheritance

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cpp:virtual_inheritance [2018/07/03 13:39] – created allsparkcpp:virtual_inheritance [2018/07/03 13:40] (current) allspark
Line 1: Line 1:
 # virtual inheritance # virtual inheritance
  
 +```cpp
 +#include <iostream>
 +#include <memory>
  
 +using namespace std;
 +struct Base
 +{
 +    Base()
 +    {
 +        cout << __PRETTY_FUNCTION__ << endl;
 +    }
 +    Base(int a)
 +    {
 +        cout << __PRETTY_FUNCTION__ << endl;
 +        cout << "a: " << a << endl;
 +    }
 +};
 +
 +struct Left : virtual Base
 +{
 +    Left() : Base()
 +    {
 +        cout << __PRETTY_FUNCTION__ << endl;
 +    }
 +    Left(int a) : Base(a)
 +    {
 +        cout << __PRETTY_FUNCTION__ << endl;
 +        cout << "a: " << a << endl;
 +    }
 +};
 +
 +struct Right : virtual Base
 +{
 +    Right() : Base()
 +    {
 +        cout << __PRETTY_FUNCTION__ << endl;
 +    }
 +    Right(int a) : Base(a)
 +    {
 +        cout << __PRETTY_FUNCTION__ << endl;
 +        cout << "a: " << a << endl;
 +    }
 +};
 +
 +struct Down : Left, Right
 +{
 +    Down() : Base(), Right(), Left()
 +    {
 +        cout << __PRETTY_FUNCTION__ << endl;
 +    }
 +    Down(int a) : Base(a+1), Right(a+2), Left(a+3)
 +    {
 +        cout << __PRETTY_FUNCTION__ << endl;
 +        cout << "a: " << a << endl;
 +    }
 +};
 +
 +int main()
 +{
 +    std::cout << "Hello, World!" << std::endl;
 +
 +    auto ptr = std::make_unique<Down>();
 +    auto ptr2 = std::make_unique<Down>(1);
 +
 +    return 0;
 +}
 +```
 +
 +```output
 +Hello, World!
 +Base::Base()
 +Left::Left()
 +Right::Right()
 +Down::Down()
 +Base::Base(int)
 +a: 2
 +Left::Left(int)
 +a: 4
 +Right::Right(int)
 +a: 3
 +Down::Down(int)
 +a: 1
 +```
cpp/virtual_inheritance.1530617970.txt.gz · Last modified: by allspark