I'm trying to understand the behaviour of this code:
class A {
public:
int a;
A(int x = 0): a(x) {cout << "A-ctor" << endl;}
~A() {cout << "A-dtor" << endl;}
};
class B {
public:
int b;
B(int x = 0): b(x) {cout << "B-ctor" << endl;}
~B() {cout << "B-dtor" << endl;}
};
int main() {
A a;
B b;
a = b.b;
return 0;
}
The output of this program is:
A-ctor
B-ctor
A-ctor
A-dtor
B-dtor
A-dtor
My question is, in this line in main(): a = b.b, an instance of class A is created and immediately destroyed. Why is this happening? Moreover, shouldn't I get a compilation error when trying to assign an object of one class to a different class?