我应该在纯虚拟类中使用析构函数吗?什么时候? [英] Shall I use destructor in a pure virtural class?And when?

查看:80
本文介绍了我应该在纯虚拟类中使用析构函数吗?什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,伙计们:
最近,我有一个关于纯虚拟类的问题.有时我需要为我的工作设计一个界面,如下所示:

Hello, guys:
Recently, I got a question, about pure virtual class. Sometimes I need to design an interface for my job, looks like:

class CInterface
{
    virtual ~CInterface()=0{}
    virtual void SomeAction()=0;
}

class DerivedA:public CInterface
{
   DerivedA(){}
   ~DerivedA(){}
   void SomeAction()
   {
   cout<<"Action from Derived class A ."<<endl;
   }
}


但是令我感到困惑的是,在我更改了CInterface之后它可以很好地工作,如下所示:


But what make me confused is that it works well after I change CInterface,like below:

class CInterface
{
    //virtual ~CInterface()=0{}
    virtual void SomeAction()=0;
}



所以,我的问题是,我应该在纯虚拟类中使用析构函数吗?何时?

提前谢谢!

[ADD]:

首先,非常感谢大家!
然后,我们应该继续处理此问题.在我看来,我们设计接口或纯虚拟类的目的是将其用作接口或指针(存在一些指向DerivedA对象的地址).
1.因此,我们这样做:



So, my question is, shall I use destructor in a pure virtural class?And when?

Thanks in advance!

[ADD]:

Firstly, thanks a lot to all of you!
Then we should continue about this issue. In my opinion, what we design an interface or a pure virtual class for is use it as an interface or pointer(some address pointed to DerivedA object existed).
1. So, we do it like this:

void SomeFun(CInterface *pt)//we always pass a DerivedA* to pt.
{
pt->SomeAction();//polymorphism 
}


2.不能这样使用:


2. Not used like this:

CInterface *pt = new DerivedA();
pt->SomeAction();
delete pt;


因为我们没有将CInterface设计为DerivedA的基"类,而只是一个接口,所以如果这样,我们应该将其命名为"DerivedABaseClass"或其他.

为了使类CInterface成为真实的接口,我们仅将其用作客户端类中的句柄",切勿在"CInterface *"指针上分配或取消分配.


3.因此,我认为应该取消CInterface类中的析构函数.

4.欢迎进行讨论,任何人都可以将问题弄清楚.


Because we don''t design CInterface as a ''base'' class for DerivedA but only an interface,if so ,we should call it ''DerivedABaseClass'' or sth.

To make the class CInterface be a real interface, we only use it as a ''handle'' in client classes,we should never allocate or deallocate upon ''CInterface*'' pointers.


3. So, I think the destructor in class CInterface should be canceled.

4. Welcome for discussing, anyone make the issue clear will be appreciated.

推荐答案

仅在需要释放指针(或空闲资源)的情况下才需要析构函数.
You only need a destructor if you need to deallocate pointers (or free resources) that were allocated within the pure virtual class itself.


我认为您已将CInterface的析构函数声明为private(默认声明),因此这就是您的第一个代码不起作用的原因.评论它编译器为您生成了默认析构函数.

但据我所知,您应该在Interface(Generic Class)中声明虚拟destrucor.在此示例中将解释此析构函数的使用,
I think you declared the destructor of CInterface as private(default declaration), so that is why your first code did not worked.In the second since you commented it compiler generated the default destructor for you.

But As for as i know you should declare virtual destrucor and in the Interface(Generic Class).The use of this destructor will be explained in this example,
class CInterface
{
public:
    virtual ~CInterface() = 0{};
    virtual void SomeAction()=0;
};
class DerivedA:public CInterface
{
public:
   DerivedA(){}
   ~DerivedA(){cout<<"In Deriveda"<<endl;}
   void SomeAction()
   {
       cout<<"Action from Derived class A ."<<endl;
   }
};
int main()
{
    CInterface* A = new DerivedA;
    delete A;
    return 0;
}


因此,如果删除A指针,则编译器会自动调用CInterface析构函数;如果不将其声明为虚拟或完全未声明,则不会调用CInterface析构函数.
至于我知道虚拟〜CInterface()= 0;和虚拟〜CInterface();在一个接口中是相同的,只有一个差异.如果要使一个类纯虚拟并且该类没有任何功能,则可以将析构函数设为纯虚拟,以便整个类都变为虚拟.希望您理解.


So if you delete A pointer the CInterface destructor will be called automatically by the compiler.if you wont decalre it as virtual or not declared at all thE CInterface destructor will not be called.

As for as i know virtual ~CInterface() = 0; and virtual ~CInterface(); are same in a Interface with only one differance.i.e. If you want to make a class pure virtual and that class is not having any functions then you can make the destructor as pure virtual, so that the whole class become virtual.I hope you understood.


内曼贾已经给出了很好的答案.如果您的基类未定义虚拟析构函数,而继承的类实现了析构函数,则您将遇到他描述的问题:如果要删除的对象不是来自该确切类,则将不会调用派生的析构函数(基类指针).

简而言之,我会说:始终在基本抽象类中定义一个虚拟析构函数.
-如果以派生类型实现析构函数,则在任何情况下都将调用此析构函数.
-如果您在继承的类中不需要析构函数,则不会有任何伤害.
Nemanja already gave the good answer. If your base class doesn''t define a virtual destructor and your inherited classes implement a destructor, then you will encounter the problem he described: your derived destructors will not be called if the object you are deleting is not from that exact class (a base class pointer).

To make short I would say: always define a virtual destructor in your base abstract classes.
- If you implement a destructor in a derived type, this destructor will be called in any case.
- If you don''t need a destructor in your inherited classes, this will not hurt.


这篇关于我应该在纯虚拟类中使用析构函数吗?什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆