User Tools

Site Tools


cpp:virtual_inheritance

virtual inheritance

snippet.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;
}
snippet.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.txt · Last modified: by allspark