cpp/object_oriented/why_destructor_be_virtual #59
Replies: 2 comments 2 replies
-
class Base {
public:
virtual ~Base() {
std::cout << "Base destructor called." << std::endl;
}
};
class Derived : public Base {
public:
~Derived() {
std::cout << "Derived destructor called." << std::endl;
}
};
int main() {
Base* ptr = new Derived();
delete ptr;
return 0;
} 这段代码派生类析构函数没有加 virtual 真的会调用派生类的析构函数吗?不应该只调用基类的构造函数吗? |
Beta Was this translation helpful? Give feedback.
1 reply
-
Effective C++条款07 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
cpp/object_oriented/why_destructor_be_virtual
编程指北: 计算机系统学习指南, 操作系统, 计算机网络, C++, Java, 算法, 数据结构
https://csguide.cn/cpp/object_oriented/why_destructor_be_virtual.html
Beta Was this translation helpful? Give feedback.
All reactions