BUG:编译器允许在没有编译析构函数的情况下创建对象 [英] BUG: compiler allows for creation of objects without destructor compiled

查看:55
本文介绍了BUG:编译器允许在没有编译析构函数的情况下创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码:


#include< iostream>


班级基础

{

私人:

虚拟~base()

{

std :: cout<< " virtual~base()\ n";

}

};


派生类:公共基础//必须导致编译时错误

{

//在取消注释时确实会导致编译时错误

//〜derived(){ }

};


int main()

{

//确实导致了取消注释时编译时错误

//派生dd;


派生* d =新派生;

删除d;

}


编译时认为必须产生错误。它只产生2

警告:


警告C4624:''衍生'':无法生成析构函数因为

基类析构函数不可访问

警告C4527:类''derived''的实例永远不会被销毁 -

用户定义的析构函数需要


第二个警告是假的 - 代码确实创建了派生的实例
,更糟糕的是它会破坏它。在运行时,它看起来像是基础的'

析构函数没有被调用。


-

Maxim Yegorushkin
MetaCommunications Engineering
http://www.meta- comm.com/engineering/

The following code:

#include <iostream>

class base
{
private:
virtual ~base()
{
std::cout << "virtual ~base()\n";
}
};

class derived : public base // must result in a compile time error
{
// does result in a compile time error when uncommented
//~derived() {}
};

int main()
{
// does result in a compile time error when uncommented
//derived dd;

derived* d = new derived;
delete d;
}

Produces no error when compiled thought it must. It only produces 2
warnings:

warning C4624: ''derived'' : destructor could not be generated because a
base class destructor is inaccessible
warning C4527: instances of class ''derived'' can never be destroyed -
user-defined destructor required

The second warning is false - the code does create an instance of derived
and what much worse it does destroy it. At runtime it seems like base''s
destructor does not get called.

--
Maxim Yegorushkin
MetaCommunications Engineering
http://www.meta-comm.com/engineering/

推荐答案

您好,

试试这个:

class bas


public

virtual~base(


TRACE(" virtual~base() \ n")


}


派生类:公共基础//必须导致编译时间错误


//在uncommente时确实会导致编译时错误

public:

~derived(){}

}


查看您编写的代码。:您已经将析构函数设置为私有。因此它也不会在派生中显示。

这就是为什么它会给编译时错误。
Hi ,
Try this :
class bas

public
virtual ~base(

TRACE("virtual ~base()\n")

}

class derived : public base // must result in a compile time erro

// does result in a compile time error when uncommente
public :
~derived() {}
}

See the code you have written. : you have made destructor "Private ". So that it would not be visible in derived also.
That''s why it is giving compile time error.


Rudresh < RU *********** @ efi.com>写道:
Rudresh <ru***********@efi.com> wrote:
[...]
查看您编写的代码。 :你已经使析构函数私有。
因此它也不会在派生中可见。
这就是它给编译时错误的原因。
[...]
See the code you have written. : you have made destructor "Private ".
So that it would not be visible in derived also.
That''s why it is giving compile time error.



再次查看帖子。我会说:

这就是为什么它_should_给出了一个

编译时错误。

我我称这是一个错误。 Comeau也是如此:


Comeau C / C ++ 4。3。3(2003年8月6日15:13:37)ONLINE_EVALUATION_BETA1

版权所有1988-2003 Comeau Computing。保留所有权利。

模式:严格错误C ++


" ComeauTest.c",第12行:错误:" base :: ~base() "无法访问

类派生:公共基础//必须导致编译时错误

^

在隐式生成derived时检测到: :衍生()"在线

23


" ComeauTest.c",第12行:错误:" base :: ~base()"无法访问

类派生:公共基础//必须导致编译时错误

^

期间检测到:

隐式生成derived ::〜derived()第23行

隐式生成derived :: derived()第23行


在编辑ComeauTest.c中检测到2个错误。


Schobi


-
Sp******@gmx.de 永远不会被阅读

我是Schobi at suespammers dot org


有时编译器比人们更合理。

Scott Meyers


Look at the posting again. I''d say:
"That''s why it _should_ be giving a
compile-time error."
I''d call this a bug. So does Comeau:

Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 12: error: "base::~base()" is inaccessible
class derived : public base // must result in a compile time error
^
detected during implicit generation of "derived::derived()" at line
23

"ComeauTest.c", line 12: error: "base::~base()" is inaccessible
class derived : public base // must result in a compile time error
^
detected during:
implicit generation of "derived::~derived()" at line 23
implicit generation of "derived::derived()" at line 23

2 errors detected in the compilation of "ComeauTest.c".

Schobi

--
Sp******@gmx.de is never read
I''m Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers


Rudresh写道:
Rudresh wrote:
查看您编写的代码。 :你已经使析构函数私有。所以


这是我的意图。

它也不会在派生中看到。
这就是它给编译时间的原因错误。
See the code you have written. : you have made destructor "Private ". So
That is my intent.
that it would not be visible in derived also.
That''s why it is giving compile time error.




问题是它*不会*给出编译错误但必须。


-

Maxim Yegorushkin



The problem is that it *does not* give a compile error but it must.

--
Maxim Yegorushkin


这篇关于BUG:编译器允许在没有编译析构函数的情况下创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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