Is someone can tell why A a = B();
call constructor fisrt and then destructor immediately?
And why the output like this?
C A
C B
D B
D A
test1 A
D A
class A {
public:
A() {
cout<< "C A" <<endl;
}
~A() {
cout<< "D A" <<endl;
}
void test1() {
cout<< "test1 A" << endl;
}
};
class B:public A {
public:
B() {
cout<< "C B" <<endl;
}
~B() {
cout<< "D B" <<endl;
}
void test1() {
cout<< "test1 B" << endl;
}
};
int main(int argc, const char * argv[]) {
A a = B();
a.test1();
return 0;
}