您的位置:首页 > 其它

什么情况下,类的析构函数应该声明为虚函数?

2014-10-15 23:16 429 查看


什么情况下,类的析构函数应该声明为虚函数?

分类: C/C++基础2012-10-25
12:05 597人阅读 评论(0) 收藏 举报

<pre class="reply-text mb10" id="content-1011017041" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 10px; background-color: rgb(255, 255, 255); padding: 0px; font-family: Arial; zoom: 1; "><strong>析构函数的用法</strong>:
<pre class="reply-text mb10" id="content-582049157" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 10px; background-color: rgb(255, 255, 255); padding: 0px; font-family: Arial; zoom: 1; ">1.每个析构函数(不加 virtual) 只负责清除自己的成员。
2.可能有基类指针,指向的确是派生类成员的情况。



对于第二种情况:

正确执行:基类指针可以指向派生类的对象(多态性),如果删除该指针delete []p;就会<strong>调用该指针指向的派生类析构函数</strong>,而派生类的析构函数<strong>又自动调用基类的析构函数</strong>,这样整个派生类的对象完全被释放。
如果析构函数不被声明成虚函数,则编译器实施静态绑定,在删除基类指针时,<strong>只会调用基类的析构函数而不调用派生类析构函数</strong>,这样就会造成派生类对象析构不完全。所以,将析构函数声明为虚函数是十分必要的。
</pre><pre class="reply-text mb10" id="content-1011017041" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 10px; padding: 0px; font-family: Arial; zoom: 1; line-height: 24px; "><pre class="reply-text mb10" id="content-582049157" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 10px; padding: 0px; font-family: Arial; zoom: 1; background-color: rgb(255, 255, 255); ">所以要保证运行适当的析构函数,基类中的析构函数必须为虚析构。
反正你在写一个类时,将其析构函数写为虚函数总不会错,一样重载的。
</pre><pre class="reply-text mb10" id="content-582049157" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 10px; padding: 0px; font-family: Arial; zoom: 1; background-color: rgb(255, 255, 255); "><pre class="reply-text mb10" id="content-1011017041" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 10px; background-color: rgb(255, 255, 255); padding: 0px; font-family: Arial; zoom: 1; ">示例:
<span style="font-family: simsun; line-height: 23px; ">假设父类为A,子类为B,当A*   p   =   new   B();</span>
<span style="font-family: simsun; line-height: 23px; ">要析构p指向的对象时,若a中的析构函数不是virtual,则<span style="color: rgb(255, 0, 0); ">调用delete p时,不会调用b的析构函数,这样在b中分配的资源就无法释放</span>了。</span>
<span style="font-family: simsun; line-height: 23px; "><strong>会造成内存泄露 </strong></span>
<span style="font-family: simsun; line-height: 23px; ">class   A 
{ 
public: 
        virtual   ~A(   void   ); 
}; 

class   B   :   public   A 
{ 
public: 
        virtual   ~B(   void   ); 
        int   n; 
}; 

如果你这样用: 

A   *pb   =   new   B; 
然后 
delete   pb; 
<strong>如果A的析构不是虚的,那么删除A的指针pb时,调用的析构函数就只有A的析构</strong>,但如果A的析构是虚的话,删除pb时,就会从pb里的虚函数表里去查找pb真实类型的析构函数。 

如果析构函数不是虚的,那么删除pb时,只调用了A的析构,而没有调用B类的析构,所以B类中的int   n将不会被删除,造成内存泄露!</span>



<p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">注:</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">1. <span style="color: rgb(255, 0, 0); ">一个成员函数被声明为虚函数后,在同一类族中的类就不能再定义一个非virtual的但与该虚函数具有相同的参数</span>(包括个数和类型)和函数返回值类型的同名函数。</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">2. 根据什么考虑是否把一个成员函数声明为虚函数?</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">   (1)<span style="background-color: rgb(255, 255, 0); ">成员函数所在的类是否会作为基类。</span></p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">   (2)成员函数<span style="background-color: rgb(255, 255, 0); ">在类的继承后有无可能被更改功能</span>,如果希望更改其功能的,一般应该将它声明为虚函数。</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">        如果成员函数在类被继承后功能不需修改,或派生类用不到该函数,则不要把它声明为虚函数。</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">       不要仅仅考虑到作为基类而把类中的所有成员函数都声明为虚函数。</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">  (3)调用是通过对象名还是通过基类指针或引用去访问,如果是<span style="background-color: rgb(255, 255, 0); ">通过基类指针或引用去访问的</span>,则应当声明为虚函数。</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">
</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">说明:使用虚函数,系统要有一定的空间开销。当一个类带有虚函数时,编译系统会为该类构造一个虚函数表,它是一个指针数组,存放每个虚函数的入口地址。系统在进行动态关联的时间开销很少,提高了多态性的效率。</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">
</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; "> 3. 析构函数的作用是在对象撤销之前把类的对象从内存中撤销。通常系统只会执行基类的析构函数,不执行派生类的析构函数。</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">  只需要把基类的析构函数声明为虚函数,即虚析构函数,这样就会撤销基类对象的同时撤销派生类的对象了,这个过程是动态关联完成的。</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; "><span style="color: rgb(255, 0, 0); ">4.如果将基类的析构函数声明为虚函数时,由该基类所派生的所有派生类的析构函数都自动成为虚函数,即使派生类的析构函数与基类的析构函数名字不相同。</span></p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">最好把基类的析构函数声明为虚函数。这将使所有派生类的析构函数自动成为虚函数,如果程序中显式delete运算符删除一个对象,而操作对象用了指向派生类对象的基类指针,系统会调用相应类的析构函数。</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">5.<strong>构造函数不能声明为虚函数。</strong></p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; "></p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; "><strong>6.纯虚函数</strong></p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">有时候,<span style="color: rgb(255, 0, 0); ">基类中的虚函数是为了派生类中的使用而声明定义</span>的,其在基类中没有任何意义。此类函数我们叫做纯虚函数,不需要写成空函数的形式,只需要声明成:</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; "><span style="background-color: rgb(255, 255, 0); ">    virtual float area()const=0;</span></p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">一般形式:virtual 函数类型 函数名(形参表列)=0;</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">注意:<strong>纯虚函数没有函数体;</strong></p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; "><span style="color: rgb(255, 0, 0); ">最后面的“=0“并不代表函数返回值为0,只是形式上的作用,告诉编译系统”这是纯虚函数”</span>;</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">这是一个声明语句,最后应有分号。</p><p style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); line-height: 26px; ">纯虚函数只有函数的名字但不具备函数的功能,不能被调用。在派生类中对此函数提供定义后,才能具备函数的功能,可以被调用。</p><span style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); ">

</span>


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: