vendredi 31 mars 2017

Why this constructor is called twice?

I have this code:

// Example program
#include <iostream>
#include <string>

class Hello{
    public:
    Hello(){std::cout<<"Hello world!"<<std::endl;}
};

class Base{
    public:
    Base(const Hello &hello){ this->hello = hello;}
    private:
    Hello hello;
};

class Derived : public Base{
    public:
    Derived(const Hello &hello) : Base(hello) {}
};

int main()
{
    Hello hello;
    Derived d(hello);
    return 0;
}

The resulting print is:

Hello world!
Hello world!

Why this happens?

Aucun commentaire:

Enregistrer un commentaire