0
我嘗試這樣做的代碼:爲什麼C++的析構函數調用2倍
#include <iostream>
using namespace std;
class A {
public:
A(){cout<<"A();";}
~A(){cout<<"~A();";}
};
class B : public A {
public:
B(){cout<<"B();";}
~B(){cout<<"~B();";}
};
int main() {
A a =B();
return 0;
}
輸出: A(); B();〜B();〜A();〜A();
爲什麼A的析構函數調用了2次?
你有複製構造和切片... – Jarod42