操作员删除问题。 [英] operator delete problem.

查看:69
本文介绍了操作员删除问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




这是我的问题:我需要能够在删除

对象时调用虚拟方法。


A级

{

公开:

虚拟~A();

void virtual DestroyRespond();

};


B级:公共A

{

public:

virtual~B();

虚拟虚拟DestroyRespond();

};


A :: ~A()

{

DestroyRespond();

}


void A :: DestroyRespond()

{

printf(销毁A \ n);

}


B :: ~B()

{


}


void B :: DestroyRespond()

{

printf(销毁B\ n);

A :: DestroyRespond ();


}

int main(int argc,char * argv [])

{

B * b =新B();


删除b;

}

显然这将是永远不会工作,对象B的虚拟方法将会因为B的破坏而被调用。


有没有办法重载运算符删除""例如,那将是
解决我的问题。


这里的限制之一就是不能调用A :: operator delete b;我需要

这是透明的,作为框架的一部分。我也知道用一个

smart_pointer这可以解决,但我也知道B

的构造是标准的。


框架需要标准化


B * b =新B();

删除b;

输出将需要


销毁B

销毁A

谢谢。

Hi,

Here is my problem: I need to be able to call a virtual method when an
object is deleted.

class A
{
public:
virtual ~A();
void virtual DestroyRespond();
};

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

A::~A()
{
DestroyRespond();
}

void A::DestroyRespond()
{
printf("destruction of A\n");
}

B::~B()
{

}

void B::DestroyRespond()
{
printf("destruction of B\n");
A::DestroyRespond();

}
int main(int argc, char* argv[])
{
B *b = new B();

delete b;
}
obviously this will never work, the virtual method of object B will
never get called on the destruction of B.

Is there a way ""overloading the operator delete"" for example that will
solve my problem.

here is one of the limitation cannot call A::operator delete b; i need
this to be transparent as part of a framework. I also know that with a
smart_pointer this could be solve but i also neet the contruction of B
to be standart.

the framework needs to be standart

B *b = new B();
delete b;
the ouput will need to be

destruction of B
destruction of A
thanks.

推荐答案

这有效(虽然与你的不同):


#include< iostream>

using namespace std;


A级{

公开:

虚拟~A(){

DestroyRespond() ;

}


虚拟空间DestroyRespond(){

cout<< "〜A" <<结束;

}

};


B级:公开A {

public:

虚拟~B(){

DestroyRespond();

}


虚拟空虚DestroyRespond( ){

cout<< "〜B" <<结束;

}

};


int main(){

B * b =新B;

A * a =新B;

删除b;

删除a;

返回0 ;

}


你必须在~B中调用DestroyRespond。

看,对象是从最顶层的基础开始构建的构造函数,

并从最底层的析构函数开始破坏。所以,当你用
到达~A时,对象的B部分早已消失,因此类型

信息,因此虚拟A :: DestroyRespond将在~A中调用,只需

因为B不再存在。


希望有帮助


ct < RO ******* @ yahoo.com>在消息中写道

新闻:MP ************************ @ news.videotron.ca。 ..
This works (although little different than yours) :

#include <iostream>
using namespace std;

class A {
public:
virtual ~A() {
DestroyRespond();
}

virtual void DestroyRespond() {
cout << "~A" << endl;
}
};

class B : public A {
public:
virtual ~B() {
DestroyRespond();
}

virtual void DestroyRespond() {
cout << "~B" << endl;
}
};

int main() {
B* b = new B;
A* a = new B;
delete b;
delete a;
return 0;
}

you have to call DestroyRespond in ~B.
see, objects are constructed starting from the top most base constructor,
and destructed starting with the bottom most destructor. so, by the time you
get to the ~A, the B part of the object is long gone, and so is the type
info, therefore virtual A::DestroyRespond will be called in ~A, simply
because B doesn''t exist anymore.

hope that helps

"ct" <ro*******@yahoo.com> wrote in message
news:MP************************@news.videotron.ca. ..


这是我的问题:我需要能够在删除
对象时调用虚方法。

A级
{
公开:
虚拟~A();
虚拟虚拟DestroyRespond();
};

B级:公众A
{
公开:
虚拟~B();
虚拟虚拟DestroyRespond();
};

A ::〜A()
{
DestroyRespond();
}
void A :: DestroyRespond()
{
printf (毁灭A \ n);
}

B :: ~B()
{

}

void B :: DestroyRespond()
{/> printf(" B\ n的破坏);
A :: DestroyRespond();

}

int main(int argc,char * argv [])
{* B> b = new B();

删除b;
}

显然这将永远不会起作用,对象B的虚拟方法将永远不会被叫做B的破坏。

是有一种方式重载 g运营商删除"例如,那将解决我的问题。
这里的一个限制是不能调用A :: operator delete b;我需要
这是透明的框架的一部分。我也知道用一个
smart_pointer这可以解决,但我也知道B
的结构是标准的。

框架需要标准

B * b =新B();
删除b;

输出将需要销毁B
销毁A

谢谢。
Hi,

Here is my problem: I need to be able to call a virtual method when an
object is deleted.

class A
{
public:
virtual ~A();
void virtual DestroyRespond();
};

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

A::~A()
{
DestroyRespond();
}

void A::DestroyRespond()
{
printf("destruction of A\n");
}

B::~B()
{

}

void B::DestroyRespond()
{
printf("destruction of B\n");
A::DestroyRespond();

}
int main(int argc, char* argv[])
{
B *b = new B();

delete b;
}
obviously this will never work, the virtual method of object B will
never get called on the destruction of B.

Is there a way ""overloading the operator delete"" for example that will
solve my problem.

here is one of the limitation cannot call A::operator delete b; i need
this to be transparent as part of a framework. I also know that with a
smart_pointer this could be solve but i also neet the contruction of B
to be standart.

the framework needs to be standart

B *b = new B();
delete b;
the ouput will need to be

destruction of B
destruction of A
thanks.



* ct:
[虚拟清理的描述;问题]
[description of "virtual clean-up" problem]




我75%确定这实际上只是对析构函数的影响,b $ b工作,在这种情况下解决方案将是


struct A

{

virtual~A(){std :: cout<< A destroyed.\ n; }

};


struct B:A

{

virtual~B(){ std :: cout<< B destroyed.\;
};


用于销毁B对象输出两个消息,B'是第一个。


但是,有25%的可能性,或者说,你指的是施工期间动态绑定的

破坏类比,FAQ项目23.4。


如果是这样,解决方案基本相同。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:什么是最烦人的事情usenet和电子邮件?



I''m 75% sure that this is really just confusion over the way destructors
work, in which case a solution would be

struct A
{
virtual ~A() { std::cout << "A destroyed.\n"; }
};

struct B: A
{
virtual ~B() { std::cout << "B destroyed.\n"; }
};

which for destruction of a B object outputs both messages, B''s first.

However, there is a 25% chance, or thereabouts, that you''re referring to the
destruction analogoue of dynamic binding during construction, FAQ item 23.4.

And if so the solutions are essentially the same.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


ct写道:

你好,

这是我的问题:我需要能够在删除
对象时调用虚方法。


那不行。

当基类析构函数运行时,那个

对象的派生类部分有已经被摧毁了。该对象不再来自派生的

类类型,而是基类对象。

B * b = new B();
删除b;

输出将需要破坏B
破坏A

Hi,

Here is my problem: I need to be able to call a virtual method when an
object is deleted.
That won''t work.
When the base class destructor runs, the derived class portion of that
object has been destroyed already. The object is no longer from derived
class type, but is a base class object.
B *b = new B();
delete b;

the ouput will need to be

destruction of B
destruction of A




A类

{

public:

virtual~A();

void DestroyRespond();

};


B级:公共A

{

公开:

虚拟~B();

void DestroyRespond();

};


A :: ~A()

{

DestroyRespond();

}


void A :: DestroyRespond()

{

printf(销毁A\ n);

}


B :: ~B()

{

DestroyRespond();

}


void B :: DestroyRespond()

{

printf(销毁B\ n);

}


DestroyRespond函数完全在
$中调用b $ b你想要的订单。


-

Karl Heinz Buchegger
kb ****** @ gascad.at


这篇关于操作员删除问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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