从基类析构函数内部显式析构函数 [英] Explicit destructor calls from inside base class destructor
问题描述
为了节省内存,我需要摆脱虚拟
析构函数。在这个约束条件下,我陷入了一个设计,
,我需要从
基类中调用派生类的析构函数。简而言之,它可以像下面的代码一样显示。
它最终会删除''B''的成员''''两次。为什么?有没有
a的方式?
谢谢
Frank
================================================= = ===================
#include< iostream>
使用命名空间std;
struct TypeX {~TypeX(){cerr<< " destructed\\\
英寸; }
struct A {~A(); }
struct B:public A {
TypeX x;
~B();
}
A :: ~A(){
//此时,~A知道,'这个'点实际上是'' B''
//(确实如此!不需要说''开关很糟糕'' - 其他方式没有
)
delete&(static_cast< B *>(this) - > x);
}
int
main(int,char **)
{
A * a =新B;
cerr<< " before\\\
";
//某个地方,可能有人删除''a''
删除a;
cerr << " after\\\
";
}
For memory economization, I need to get rid if the virtual
destructor. Under this constraint I get caught up in a design,
where I need to call a destructor of a derived class from a
base class. Briefly it could be displayed like the code below.
It ends up deleting the member ''x'' of ''B'' twice. Why? Is there
a way around?
Thanks
Frank
================================================== ===================
#include<iostream>
using namespace std;
struct TypeX { ~TypeX() { cerr << "destructed\n"; }
struct A { ~A(); }
struct B : public A {
TypeX x;
~B();
}
A::~A() {
// at this point, ~A knows, that ''this'' points is in fact to a ''B''
// (it does! no need to say ''type-switches are bad'' - there''s no
other way)
delete &(static_cast<B*>(this)->x);
}
int
main(int, char**)
{
A* a = new B;
cerr << "before\n";
// somewhere, there might be someone deleting ''a''
delete a;
cerr << "after\n";
}
推荐答案
* frs:
为了节省内存,我需要摆脱虚拟的
析构函数。在这个约束下,我陷入了一个设计,
我需要从
基类调用派生类的析构函数。
For memory economization, I need to get rid if the virtual
destructor. Under this constraint I get caught up in a design,
where I need to call a destructor of a derived class from a
base class.
你已经说够了。
不要。
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门帖子。
问:usenet和电子邮件中最烦人的是什么?
Already you have told enough.
Don''t.
--
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?
frs写道:
为了节省内存,我需要摆脱虚拟的析构函数。在这个约束条件下,我陷入了一个设计,
我需要从一个
基类中调用派生类的析构函数。简而言之,它可以像下面的代码一样显示。
它最终会删除B的成员x两次。为什么?有没有办法?
谢谢
弗兰克
============== ==================================== ============== =====
#include< iostream>
使用命名空间std;
struct TypeX {~TypeX(){cerr<< " destructed\\\
英寸;结构A {~A();结构B:public A {
TypeX x;
~B();
}
A :: ~A(){
//在这一点上,~A知道,这个点实际上是一个''B''
//(确实如此!不需要说''类型开关很糟糕'' - 没有
其他方式)
删除&(static_cast< B *>(this) - > x);
For memory economization, I need to get rid if the virtual
destructor. Under this constraint I get caught up in a design,
where I need to call a destructor of a derived class from a
base class. Briefly it could be displayed like the code below.
It ends up deleting the member ''x'' of ''B'' twice. Why? Is there
a way around?
Thanks
Frank
================================================== ===================
#include<iostream>
using namespace std;
struct TypeX { ~TypeX() { cerr << "destructed\n"; }
struct A { ~A(); }
struct B : public A {
TypeX x;
~B();
}
A::~A() {
// at this point, ~A knows, that ''this'' points is in fact to a ''B''
// (it does! no need to say ''type-switches are bad'' - there''s no
other way)
delete &(static_cast<B*>(this)->x);
ITYM
static_cast< B *>(this) - > x :: ~TypeX();
但是实际上,我认为你应该为C ++选择一种不同的语言,如果你的内存限制与你说的一样。也许你会用C来改善
。
john
ITYM
static_cast<B*>(this)->x::~TypeX();
but really, I think you should choose a different language to C++ if you
memory constratints are as titght as you say they are. Perhaps you would
be better of with C.
john
frs写道:
frs wrote:
为了节省内存,我需要摆脱虚拟的
析构函数。在这个约束条件下,我陷入了一个设计,
我需要从一个
基类中调用派生类的析构函数。简而言之,它可以像下面的代码一样显示。
它最终会删除B的成员x两次。为什么?有没有办法?
谢谢
弗兰克
============== ==================================== ============== =====
#include< iostream>
使用命名空间std;
struct TypeX {~TypeX(){cerr<< " destructed\\\
英寸;结构A {~A();结构B:public A {
TypeX x;
~B();
}
A :: ~A(){
//在这一点上,~A知道,这个点实际上是一个''B''
//(确实如此!不需要说''类型开关很糟糕'' - 没有
其他方式)
删除&(static_cast< B *>(this) - > x);
}
> int
main(int,char **)
{A / a =新B;
cerr<< 在某些地方之前,可能会有人删除''a''
删除a;
cerr<< " after\\\
";
}
For memory economization, I need to get rid if the virtual
destructor. Under this constraint I get caught up in a design,
where I need to call a destructor of a derived class from a
base class. Briefly it could be displayed like the code below.
It ends up deleting the member ''x'' of ''B'' twice. Why? Is there
a way around?
Thanks
Frank
================================================== ===================
#include<iostream>
using namespace std;
struct TypeX { ~TypeX() { cerr << "destructed\n"; }
struct A { ~A(); }
struct B : public A {
TypeX x;
~B();
}
A::~A() {
// at this point, ~A knows, that ''this'' points is in fact to a ''B''
// (it does! no need to say ''type-switches are bad'' - there''s no
other way)
delete &(static_cast<B*>(this)->x);
}
int
main(int, char**)
{
A* a = new B;
cerr << "before\n";
// somewhere, there might be someone deleting ''a''
delete a;
cerr << "after\n";
}
如果使用以下智能,可以避免使用虚拟析构函数
指针(cow_ptr)类:
http://code.axter.com/ cow_ptr.h
然而,恕我直言,你不会通过删除虚拟的
析构函数来节省很多钱。
You can avoid using a virtual destructor if you use the following smart
pointer (cow_ptr) class:
http://code.axter.com/cow_ptr.h
However, IMHO, you''re not going to save much by removing the virtual
destructor.
这篇关于从基类析构函数内部显式析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!