C++类什么情况下需要虚拟析构函数


//特别需要指出的是,本文中的所有的测试结果都是在g++编译器和VC8编译器下测试的结果。
//各自的版本信息如下所示:
//g++
/*******************************************************************************
Reading specs from D:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/specs
Configured with: ../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as
--host=mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls
--enable-languages=c,c++,f77,ada,objc,java --disable-win32-registry
--disable-shared --enable-sjlj-exceptions --enable-libgcj --disable-java-awt
--without-x --enable-java-gc=boehm --disable-libgcj-debug --enable-interpreter
--enable-hash-synchronization --enable-libstdcxx-debug
Thread model: win32
gcc version 3.4.2 (mingw-special)
*******************************************************************************/
//VC8
/*******************************************************************************
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]
*******************************************************************************/

#if CODE1
#include <iostream>
//给出一个没有虚拟析构函数的基类
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()
{
{//堆栈变量的情况
std::cout << "----------[Derived d;]----------" << std::endl;
Derived d;
}
{//堆内存分配,并且删除派生类对象指针的情况
std::cout << "----------[Derived *pd = new Derived();]----------" << std::endl;
Derived *pd = new Derived();
std::cout << "----------[delete pd;]----------" << std::endl;
delete pd;//这里会调用派生类和基类的析构函数,虽然派生类和基类的析构函数不是虚拟的
}
{//堆内存分配,并且删除基类对象指针的情况
std::cout << "----------[Base *pb = new Derived();]----------" << std::endl;
Base *pb = new Derived();
std::cout << "----------[delete pb;]----------" << std::endl;
delete pb;//这里就不会调用派生类的析构函数
}
return 0;
}

//运行结果如下所示:
/*******************************************************************************
----------[Derived d;]----------
Base::Base()
Derived::Derived()
Derived::~Derived()
Base::~Base()
----------[Derived *pd = new Derived();]----------
Base::Base()
Derived::Derived()
----------[delete pd;]----------
Derived::~Derived()
Base::~Base()
----------[Base *pb = new Derived();]----------
Base::Base()
Derived::Derived()
----------[delete pb;]----------
Base::~Base()
*******************************************************************************/

#if 0
从上面的讨论中可以看出,只有最后一种情况才会对析构函数是否是虚拟的有要求,在
后面的讨论中将会只讨论这种情况。
#endif
#endif//CODE1
#if CODE2
#include <iostream>
//给出一个没有虚拟析构函数的基类
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;
}
virtual~Derived()
{
std::cout<<"Derived::virtual~Derived()"<<std::endl;
}
};
//下面的测试代码
int main()
{
//堆内存分配,并且删除基类对象指针的情况
std::cout << "----------[Base *pb = new Derived();]----------" << std::endl;
Base *pb = new Derived();
std::cout << "----------[delete pb;]----------" << std::endl;
delete pb;//这里就不会调用派生类的析构函数
return 0;
}

//运行结果如下所示:
/*******************************************************************************
----------[Base *pb = new Derived();]----------
Base::Base()
Derived::Derived()
----------[delete pb;]----------
Base::~Base()
*******************************************************************************/

#if 0
很明显并没有调用派生类的虚拟析构函数。
#endif
#endif//CODE2
#if CODE3
#include <iostream>
//给出一个有虚拟析构函数的基类
class Base
{
public:
Base()
{
std::cout<<"Base::Base()"<<std::endl;
}
virtual~Base()
{
std::cout<<"Base::virtual~Base()"<<std::endl;
}
};
//给出一个没有虚拟析构函数的继承类
class Derived:public Base
{
public:
Derived()
{
std::cout<<"Derived::Derived()"<<std::endl;
}
~Derived()
{
std::cout<<"Derived::~Derived()"<<std::endl;
}
};
//下面的测试代码
int main()
{
//堆内存分配,并且删除基类对象指针的情况
std::cout << "----------[Base *pb = new Derived();]----------" << std::endl;
Base *pb = new Derived();
std::cout << "----------[delete pb;]----------" << std::endl;
delete pb;//这里会调用派生类的析构函数
return 0;
}

//运行结果如下所示:
/*******************************************************************************
----------[Base *pb = new Derived();]----------
Base::Base()
Derived::Derived()
----------[delete pb;]----------
Derived::~Derived()
Base::virtual~Base()
*******************************************************************************/

#if 0
很明显调用派生类的虚拟析构函数。但是这里值得注意的是:派生类的析构函数并不
是虚拟的,为了说明派生类的虚拟析构函数是否一定要是虚拟的,还需要更多的测试。
#endif
#endif//CODE3
#if CODE4
#include <iostream>
//给出一个有虚拟析构函数的基类
class Base
{
public:
Base()
{
std::cout<<"Base::Base()"<<std::endl;
}
virtual~Base()
{
std::cout<<"Base::virtual~Base()"<<std::endl;
}
};
//给出一个有虚拟析构函数的继承类
class Derived:public Base
{
public:
Derived()
{
std::cout<<"Derived::Derived()"<<std::endl;
}
virtual~Derived()
{
std::cout<<"Derived::virtual~Derived()"<<std::endl;
}
};
//下面的测试代码
int main()
{
//堆内存分配,并且删除基类对象指针的情况
std::cout << "----------[Base *pb = new Derived();]----------" << std::endl;
Base *pb = new Derived();
std::cout << "----------[delete pb;]----------" << std::endl;
delete pb;//这里会调用派生类的析构函数
return 0;
}

//运行结果如下所示:
/*******************************************************************************
----------[Base *pb = new Derived();]----------
Base::Base()
Derived::Derived()
----------[delete pb;]----------
Derived::virtual~Derived()
Base::virtual~Base()
*******************************************************************************/

#if 0
很明显调用派生类的虚拟析构函数。通过CODE3和CODE4可以看出只要基类的析构函数
是虚拟的则派生类的析构函数不论是否是虚拟的都将在删除基类指针的时候调用派生类的
析构函数。不过CODE3和CODE4仅仅讨论的是一次一重继承的情况。对于多次多重继承的情
况则需要另外的单独讨论。还是先讨论多次继承的情况,然后再讨论多重继承的情况,最
后讨论多次多重继承的情况。
#endif
#endif//CODE4
#if CODE5
//测试多次继承的情况
#include <iostream>
//给出一个有虚拟析构函数的基类
class Base
{
public:
Base()
{
std::cout<<"Base::Base()"<<std::endl;
}
virtual~Base()
{
std::cout<<"Base::virtual~Base()"<<std::endl;
}
};
//给出一个没有虚拟析构函数的继承类
class Derived1:public Base
{
public:
Derived1()
{
std::cout<<"Derived1::Derived1()"<<std::endl;
}
~Derived1()
{
std::cout<<"Derived1::~Derived1()"<<std::endl;
}
};
//给出另一个没有虚拟析构函数的继承类
class Derived2:public Derived1
{
public:
Derived2()
{
std::cout<<"Derived2::Derived2()"<<std::endl;
}
~Derived2()
{
std::cout<<"Derived2::~Derived2()"<<std::endl;
}
};
//下面的测试代码
int main()
{
//堆内存分配,并且删除基类对象指针的情况
{
std::cout << "----------[Base *pb = new Derived2();]----------" << std::endl;
Base *pb = new Derived2();
std::cout << "----------[delete pb;]----------" << std::endl;
delete pb;//这里会调用派生类的析构函数
}
{
std::cout << "----------[Base *pd1 = new Derived1();]----------" << std::endl;
Derived1 *pd1 = new Derived2();
std::cout << "----------[delete pd1;]----------" << std::endl;
delete pd1;//这里会调用派生类的析构函数
}
return 0;
}

//运行结果如下所示:
/*******************************************************************************
----------[Base *pb = new Derived2();]----------
Base::Base()
Derived1::Derived1()
Derived2::Derived2()
----------[delete pb;]----------
Derived2::~Derived2()
Derived1::~Derived1()
Base::virtual~Base()
----------[Base *pd1 = new Derived1();]----------
Base::Base()
Derived1::Derived1()
Derived2::Derived2()
----------[delete pd1;]----------
Derived2::~Derived2()
Derived1::~Derived1()
Base::virtual~Base()
*******************************************************************************/

#if 0
很明显调用了所有派生类的虚拟析构函数。但是这里值得注意的是:派生类的析构函
数并不是虚拟的。可以看出只要有基类的析构函数是虚拟的,那么所有的派生类不管是否
明确的写了虚拟析构函数,派生了的析构函数一定是虚拟的。
#endif
#endif//CODE5
#if CODE6
//测试多重继承的情况
#include <iostream>
//给出一个有虚拟析构函数的基类
class Base1
{
public:
Base1()
{
std::cout<<"Base1::Base1()"<<std::endl;
}
virtual~Base1()
{
std::cout<<"Base1::virtual~Base1()"<<std::endl;
}
};
class Base2
{
public:
Base2()
{
std::cout<<"Base2::Base2()"<<std::endl;
}
virtual~Base2()
{
std::cout<<"Base2::virtual~Base2()"<<std::endl;
}
};
//给出一个没有虚拟析构函数的继承类
class Derived:public Base1,public Base2
{
public:
Derived()
{
std::cout<<"Derived::Derived()"<<std::endl;
}
~Derived()
{
std::cout<<"Derived::~Derived()"<<std::endl;
}
};
//下面的测试代码
int main()
{
//堆内存分配,并且删除基类对象指针的情况
{
std::cout << "----------[Base *pb = new Derived();]----------" << std::endl;
Base1 *pb = new Derived();
std::cout << "----------[delete pb;]----------" << std::endl;
delete pb;//这里会调用派生类的析构函数
}
{
std::cout << "----------[Base *pb = new Derived();]----------" << std::endl;
Base2 *pb = new Derived();
std::cout << "----------[delete pb;]----------" << std::endl;
delete pb;//这里会调用派生类的析构函数
}
return 0;
}

//运行结果如下所示:
/*******************************************************************************
----------[Base *pb = new Derived();]----------
Base1::Base1()
Base2::Base2()
Derived::Derived()
----------[delete pb;]----------
Derived::~Derived()
Base2::virtual~Base2()
Base1::virtual~Base1()
----------[Base *pb = new Derived();]----------
Base1::Base1()
Base2::Base2()
Derived::Derived()
----------[delete pb;]----------
Derived::~Derived()
Base2::virtual~Base2()
Base1::virtual~Base1()
*******************************************************************************/

#if 0
很明显调用了所有派生类的析构函数和基类的析构函数。另外还可以看出只要有了多重
继承的任意一个基类的指针都可以通过这个指针将整个对象删除。另外还需要考虑一下某个
基类的析构函数不是虚拟的情况。
#endif
#endif//CODE6
#if CODE7
//测试多重继承的情况
#include <iostream>
//给出一个有虚拟析构函数的基类
class Base1
{
public:
Base1()
{
std::cout<<"Base1::Base1()"<<std::endl;
}
virtual~Base1()
{
std::cout<<"Base1::virtual~Base1()"<<std::endl;
}
};
class Base2
{
public:
Base2()
{
std::cout<<"Base2::Base2()"<<std::endl;
}
~Base2()
{
std::cout<<"Base2::~Base2()"<<std::endl;
}
};
//给出一个没有虚拟析构函数的继承类
class Derived:public Base1,public Base2
{
public:
Derived()
{
std::cout<<"Derived::Derived()"<<std::endl;
}
~Derived()
{
std::cout<<"Derived::~Derived()"<<std::endl;
}
};
//下面的测试代码
int main()
{
//堆内存分配,并且删除基类对象指针的情况
{
std::cout << "----------[Base *pb = new Derived();]----------" << std::endl;
Base1 *pb = new Derived();
std::cout << "----------[delete pb;]----------" << std::endl;
delete pb;//这里会调用派生类的析构函数
}
{
std::cout << "----------[Base *pb = new Derived();]----------" << std::endl;
Base2 *pb = new Derived();
std::cout << "----------[delete pb;]----------" << std::endl;
delete pb;//这里不会调用派生类的析构函数
}
return 0;
}

//运行结果如下所示:
/*******************************************************************************
----------[Base *pb = new Derived();]----------
Base1::Base1()
Base2::Base2()
Derived::Derived()
----------[delete pb;]----------
Derived::~Derived()
Base2::~Base2()
Base1::virtual~Base1()
----------[Base *pb = new Derived();]----------
Base1::Base1()
Base2::Base2()
Derived::Derived()
----------[delete pb;]----------
Base2::~Base2()
*******************************************************************************/

#if 0
很明显,是否调用所有派生类的析构函数和基类的析构函数,取决于删除的基类指针的
基类的析构函数是否是虚拟的,如果指针的析构函数是虚拟的,那么将会调用所有的派生类
的析构函数和所有的其它基类的析构函数,否则就会象上面的第二种情况一样仅仅删除了基
类自身而已,导致对象没有完全释放。
#endif
#endif//CODE7
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值