这个问题可能听起来太傻了,但是,我找不到具体的答案。
对于后期绑定的工作原理以及继承中使用的虚拟关键字知之甚少。
与代码示例一样,在继承的情况下,指向在heap和delete运算符上创建的派生类对象的基类指针用于释放内存,派生和基类的析构函数将被调用仅在基础析构函数声明为虚函数时才按顺序。
现在我的问题是:
1)当base的析构函数不是虚拟的时,为什么只有在使用“delete”运算符的情况下才会出现不调用派生dtor的问题,为什么不在下面给出的情况下:
derived drvd;
base *bPtr;
bPtr = &drvd; //DTOR called in proper order when goes out of scope.
2) When "delete" operator is used, who is reponsible to call the destructor of the class? The operator delete will have an implementation to call the DTOR ? or complier writes some extra stuff ? If the operator has the implementation then how does it looks like , [I need sample code how this would have been implemented].
3) If virtual keyword is used in this example, how does operator delete now know which DTOR to call?
Fundamentaly i want to know who calls the dtor of the class when delete is used.
<h1> Sample Code </h1>
class base
{
public:
base(){
cout<<"Base CTOR called"<<endl;
}
virtual ~base(){
cout<<"Base DTOR called"<<endl;
}
};
class derived:public base
{
public:
derived(){
cout<<"Derived CTOR called"<<endl;
}
~derived(){
cout<<"Derived DTOR called"<<endl;
}
};
I'm not sure if this is a duplicate, I couldn't find in search.
int main()
{
base *bPtr = new derived();
delete bPtr;// only when you explicitly try to delete an object
return 0;
2) When "delete" operator is used, who is reponsible to call the destructor of the class? The operator delete will have an implementation to call the DTOR ? or complier writes some extra stuff ? If the operator has the implementation then how does it looks like , [I need sample code how this would have been implemented]. 3) If virtual keyword is used in this example, how does operator delete now know which DTOR to call? Fundamentaly i want to know who calls the dtor of the class when delete is used. I'm not sure if this is a duplicate, I couldn't find in search. int main()
{
base *bPtr = new derived();
<h1> Sample Code </h1>
class base
{
public:
base(){
cout<<"Base CTOR called"<<endl;
}
virtual ~base(){
cout<<"Base DTOR called"<<endl;
}
};
class derived:public base
{
public:
derived(){
cout<<"Derived CTOR called"<<endl;
}
~derived(){
cout<<"Derived DTOR called"<<endl;
}
};
答案 0 :(得分:2)
这是因为在这种情况下编译器知道要被破坏的对象的所有内容,在这种情况下是drvd
并且类型为derived
。当drvd
超出范围时,编译器会插入代码来调用其析构函数
delete
是编译器的关键字。当编译器看到删除时,它会插入代码来调用析构函数和代码来调用operator delete
来释放内存。请记住,keyword delete
和operater delete
是不同的。
当编译器看到keyword delete
被用于指针时,它需要为其正确的销毁生成代码。为此,它需要知道指针的类型信息。编译器对指针的唯一了解是指针类型,而不是指针指向的对象类型。指针指向的对象可以是基类或派生类。在某些情况下,可以非常清楚地定义对象的类型,例如
void fun() { Base *base= new Derived(); delete base; }
但在大多数情况下,它不是,例如这一个
void deallocate(Base *base)
{
delete base;
}
因此编译器不知道调用base或派生的析构函数。这是它的工作方式
答案 1 :(得分:2)
+1好问题BTW。
看看虚拟机制如何为非析构函数方法工作,你会发现析构函数的行为没有区别。
有两种机制可能会使问题稍微混淆 。 首先,在构造和销毁对象时发生非虚拟机制。对象从基类到派生类按顺序构造,并且当被破坏时,析构函数顺序是相反的,因此派生到基类。这里没什么新鲜的。
考虑在基于派生类对象的类指针上调用非虚方法,会发生什么?调用基类实现。现在考虑从基类指针到派生类对象调用虚方法,会发生什么?调用方法的派生版本。没什么你不知道的。
现在让我们考虑析构函数场景。在对具有非虚析构函数的派生类对象的基类指针上调用delete。调用基类析构函数,如果基类是从另一个类派生的,那么它的析构函数将被调用。因为虚拟机制不起作用,所以不会调用派生的析构函数,因为破坏从你在层次结构中调用的析构函数开始,然后一直向下运行到基类。
现在考虑虚拟析构函数案例。对基于派生类对象的类指针调用delete。在基类指针上调用任何虚方法时会发生什么?派生版本被调用。所以我们的派生类析构函数被调用。在破坏过程中会发生什么,对象从派生的析构函数中析构到基类,但是这次我们因为虚方法机制而在派生类级别开始销毁。
为什么带有非虚拟或虚拟析构函数的堆栈对象在超出范围时会从派生到基类中销毁?因为在这种情况下调用声明的类的析构函数,并且虚拟机制与它无关。
答案 2 :(得分:1)
编译器生成所有必要的代码,以正确的顺序调用析构函数,无论是堆栈对象还是超出范围的成员变量,还是要删除的堆对象。
答案 3 :(得分:1)
如果您想了解更多信息,请尝试重载新内容并删除。