关于下面的示例代码,为什么基类的析构函数被调用了两次?
class Base {
public:
Base() {
std::cout << "Base::Base()" << std::endl;
}
~Base() {
std::cout << "Base::~Base()" << std::endl;
}
};
class Derived : public Base {
public:
Derived() {
std::cout << "Derived::Derived()" << std::endl;
}
~Derived() {
std::cout << "Derived::~Derived()" << std::endl;
}
};
int main() {
Base a = Derived();
return EXIT_SUCCESS;
}
以下是程序运行时的输出示例:
Base::Base()
Derived::Derived()
Derived::~Derived()
Base::~Base()
Base::~Base()