如何使用虚拟析构函数? [英] how to use the virtual destructor?

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

问题描述

大家好,

我有一段这样的代码

构造函数:


IPCClientDataTransfer :: IPCClientDataTransfer(RWCString serviceName):有效(FALSE)

{

NSTcpProfile tcpProfile;

int clientPriority = 1;

tcpProfile。 BlockingOnConnect = TRUE;

tcpProfile.Blocking = TRUE;

strcpy(tcpProfile.ServiceName,serviceName);

strcpy(tcpProfile.TcpHostName," ; localhost");


this - > PClient =新的IPCAMAClient(& tcpProfile,clientPriority);


//检查IPCAMAClient是否是有效对象

if(!this - > ; PClient - > isValid())

{

//对象无效,连接未建立

删除此 - > ; PClient;

this - > PClient = NULL;

}

else

{

//对象有效

this - >有效= TRUE;

}

析构函数:


IPCClientDataTransfer :: ~IPCClientDataTransfer()

{

if(this - > PClient)

删除此 - > PClient;

}


析构函数不是虚拟的。这意味着会有内存泄漏吗?

Hi all,
I have a piece of code like this
constructor:

IPCClientDataTransfer::IPCClientDataTransfer (RWCString serviceName) :Valid(FALSE)
{
NSTcpProfile tcpProfile;
int clientPriority = 1;
tcpProfile.BlockingOnConnect = TRUE;
tcpProfile.Blocking = TRUE;
strcpy(tcpProfile.ServiceName, serviceName);
strcpy(tcpProfile.TcpHostName, "localhost");

this -> PClient = new IPCAMAClient (&tcpProfile, clientPriority);

// Check to see if the IPCAMAClient is a valid object
if (!this -> PClient -> isValid())
{
// The object is invalid, the connection was not established
delete this -> PClient;
this -> PClient = NULL;
}
else
{
// The Object is valid
this -> Valid = TRUE;
}
destructor:

IPCClientDataTransfer::~IPCClientDataTransfer ()
{
if (this -> PClient)
delete this -> PClient;
}

the destructor is not virtual.does this means that there will be memory leak?

推荐答案

IPCClientDataTransfer类中没有泄漏。


但是,如果IPCClientDataTransfer具有虚函数而另一个类派生自IPCClientDataTransfer,则可能有泄漏。 />

在这种情况下,派生类对象由基类引用或基类指针(简单C ++多态)标识。如果没有IPCClientDataTransfer中的虚拟析构函数,当删除指向或引用IPCClientDataTransfer时,将不会调用派生类的析构函数。
There will be no leak in the IPCClientDataTransfer class.

However, if IPCClientDataTransfer has virtual functions and another class derives from IPCClientDataTransfer, you may have a leak.

In this case the derived class object is identified by either a base class reference or a base class pointer (simple C++ polymorphism). Without the virtual destructor in IPCClientDataTransfer, the destructor of the derived class will not be called when the pointer or reference to IPCClientDataTransfer is deleted.


ok.t​​hanks很多。

在IPCClientDataTransfer.C类中没有虚函数。
此行中的


this - > PClient =新的IPCAMAClient(& tcpProfile,clientPriority);


a为IPCAMAClient类创建了新实例。

但我不认为它是派生类IPCClientDataTransfer.C类。


是否仍存在内存泄漏问题,因为IPCClientDataTransfer.C类析构函数不是虚拟的?
ok.thanks a lot.
there are no virtual functions in IPCClientDataTransfer.C class.
in this line
this -> PClient = new IPCAMAClient (&tcpProfile, clientPriority);

a new instance has been created for IPCAMAClient class.
but i dont think it is a derived class of IPCClientDataTransfer.C class.

whether still the memory leak problem exist because the IPCClientDataTransfer.C class destructor is not virtual?


还有一个info


IPCClientDataTransfer是IPCAMAClient的包装类。
and one more info

IPCClientDataTransfer is wrapper class around IPCAMAClient.


这篇关于如何使用虚拟析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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