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 2warnings:warning C4624: ''derived'' : destructor could not be generated because abase class destructor is inaccessiblewarning C4527: instances of class ''derived'' can never be destroyed -user-defined destructor requiredThe second warning is false - the code does create an instance of derivedand what much worse it does destroy it. At runtime it seems like base''sdestructor does not get called.--Maxim YegorushkinMetaCommunications Engineering http://www.meta-comm.com/engineering/推荐答案您好, 试试这个: class bas public virtual~base( TRACE(" virtual~base() \ n") } 派生类:公共基础//必须导致编译时间错误 //在uncommente时确实会导致编译时错误 public: ~derived(){} } 查看您编写的代码。:您已经将析构函数设置为私有。因此它也不会在派生中显示。 这就是为什么它会给编译时错误。Hi ,Try this :class baspublicvirtual ~base(TRACE("virtual ~base()\n")}class derived : public base // must result in a compile time erro// does result in a compile time error when uncommentepublic :~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 MeyersLook at the posting again. I''d say:"That''s why it _should_ be giving acompile-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_BETA1Copyright 1988-2003 Comeau Computing. All rights reserved.MODE:strict errors C++"ComeauTest.c", line 12: error: "base::~base()" is inaccessibleclass derived : public base // must result in a compile time error^detected during implicit generation of "derived::derived()" at line23"ComeauTest.c", line 12: error: "base::~base()" is inaccessibleclass derived : public base // must result in a compile time error^detected during:implicit generation of "derived::~derived()" at line 23implicit generation of "derived::derived()" at line 232 errors detected in the compilation of "ComeauTest.c".Schobi-- Sp******@gmx.de is never readI''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 ". SoThat is my intent. that it would not be visible in derived also. That''s why it is giving compile time error. 问题是它*不会*给出编译错误但必须。 - Maxim YegorushkinThe problem is that it *does not* give a compile error but it must.--Maxim Yegorushkin 这篇关于BUG:编译器允许在没有编译析构函数的情况下创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 22:54