J
Juha Nieminen
Consider the following code:
//---------------------------------------------------------------
#include <iostream>
struct Base
{
Base() { std::cout << "Base constructor\n"; }
Base(const Base&) { std::cout << "Base copy constructor\n"; }
Base& operator=(const Base&)
{ std::cout << "Base operator=\n"; return *this;}
};
struct M1: virtual public Base {};
struct M2: virtual public Base {};
struct Derived: public M1, public M2 {};
int main()
{
Derived d1;
Derived d2(d1);
d1 = d2;
}
//---------------------------------------------------------------
One would assume that this program prints one message of each type.
However, a bit surprisingly, this is the result:
Base constructor
Base copy constructor
Base operator=
Base operator=
Why is that?
I'm using gcc 4.3.1.
//---------------------------------------------------------------
#include <iostream>
struct Base
{
Base() { std::cout << "Base constructor\n"; }
Base(const Base&) { std::cout << "Base copy constructor\n"; }
Base& operator=(const Base&)
{ std::cout << "Base operator=\n"; return *this;}
};
struct M1: virtual public Base {};
struct M2: virtual public Base {};
struct Derived: public M1, public M2 {};
int main()
{
Derived d1;
Derived d2(d1);
d1 = d2;
}
//---------------------------------------------------------------
One would assume that this program prints one message of each type.
However, a bit surprisingly, this is the result:
Base constructor
Base copy constructor
Base operator=
Base operator=
Why is that?
I'm using gcc 4.3.1.