草庐IT

virtual-destructor

全部标签

c++ - 规则 "A user-defined but do-nothing destructor is also a non-trivial destructor"是否过于严格?

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。ImprovethisquestionclassBase{public:~Base(){}private:intval;};Basebase;//aglobalvariable你看,析构函数什么都不做,和c++编译器提供的默认析构函数一样。但是根据帖子Whatisanon-trivialdestructorinC++?,析构函数仍然是一个非平凡的析构函数我知道,标准中的每条规则都必须严格。但是,根据我粘贴的上面的代码,用户定义

c++ - __attribute__((destructor)) 在 VC 中等效?

我看过__attribute__((constructor))equivalentinVC?和CRTInitialization,这对特定于gcc的__attribute__((constructor))都有帮助。但是__attribute__((destructor))呢?是否有等效的VC? 最佳答案 如果你正在制作一个动态链接库,你可以制作你的DllMainentrypoint处理这个:BOOLWINAPIDllMain(HINSTANCEhinstDLL,DWORDfdwReason,LPVOIDlpvReserved){if

c++ - 海湾合作委员会 : C++11 inline object initialization (using "this") does not work when there is a virtual inheritance in hierarchy

当在初始化中使用此指针并且在层次结构中存在虚拟继承时,C++11内联对象初始化不起作用(在GCC中)。这可能是GCC的错误吗(因为它在CLang中工作)?还是C++11标准本身的差距?示例(可以在here中尝试),当使用GCC编译以下代码时:FieldIndexm_inB{"inB",this};不会被执行。但它会在使用CLang编译时执行。变通方法:从FieldIndexContainer派生A作为虚拟#include#include#includeusingnamespacestd;classFieldIndexContainer{public:classFieldIndex{pu

c++ - 使用 protected 非虚拟析构函数时抑制 delete-non-virtual-dtor 警告

我有一个纯抽象接口(interface)类和一个实现该接口(interface)的派生类。structFoo{virtualvoiddoStuff()=0;};structBar:Foo{voiddoStuff()override{}};我的接口(interface)类没有虚拟析构函数。因此,尝试使用基类指针破坏派生实例显然是未定义的行为intmain(){Foo*f=newBar;f->doStuff();deletef;}幸运的是我的编译器足够聪明,可以捕捉到这个(使用-Werror)main.cc:15:9:error:deletingobjectofabstractclasst

C++ 销毁顺序 : Calling a field destructor before the class destructor

有没有办法在类析构函数之前调用字段析构函数?假设我有2个类Small和Big,Big包含一个Small的实例作为它的字段因此:classSmall{public:~Small(){std::cout当然,这会在小析构函数之前调用大析构函数:BigdestructorSmalldestructor我需要在Big析构函数之前调用Small析构函数,因为它会为Big析构函数执行一些必要的清理工作。我可以:显式调用small.~Small()析构函数。->但是,这会调用Small析构函数两次:一次显式调用,一次在Big析构函数执行后调用。有一个Small*作为字段并在Big析构函数中调用del

c++ - 如何解决 : undefined reference to `non-virtual thunk to ...`

我正在尝试弄清楚如何进一步解决此问题。我还想知道如何安装更新版本的ld(如果有意义的话)。所有涉及的包管理器都告诉我,我是最新的。代码在ubuntu12.04和12.10上使用g++(4.7.2)编译、链接和运行,但在FC17上编译失败并出现此错误。ArchiveServiceLib/debug-posix/libArchiveLib.a(NamedIflTiffCache.o):(.rodata._ZTV26UnlockingGenericFileHandle[_ZTV26UnlockingGenericFileHandle]+0x58):undefinedreferenceto`I

C++ : noexcept (or throw()) virtual destructor = default;

下面的代码是合法的吗?classC{virtual~C()noexcept=default;};或classC{virtual~C()throw()=default;};(throw()已弃用,但我的编译器不支持noexcept;;) 最佳答案 8.4.2[dcl.fct.def.default]Anexplicitly-defaultedfunction[...]mayhaveanexplicitexception-specificationonlyifitiscompatible(15.4)withtheexception-spe

c++ - 如何在不破坏移动和复制构造函数的情况下声明虚拟析构函数

当向这样的类添加用户定义的默认虚拟析构函数时..classFoo{public:Foo();virtual~Foo()=default;};..它具有阻止自动生成移动构造函数的副作用。复制构造函数的自动生成也被弃用。Arecommendedwayistouserdefineallconstructors像这样..classFoo{public:Foo();virtual~Foo()=default;Foo(constFoo&/*other*/)=default;Foo&operator=(constFoo&/*other*/)=default;Foo(Foo&&/*other*/)=d

c++ - Destructor间接调用虚函数

让我声明一下:我对Constructor或Destructor中的虚函数调用有清楚的理解。在下面的代码中,我试图避免仅出于实验目的的虚拟析构函数。现在我的问题是:在main中,对Destroyfun的调用调用了正确的虚函数。我期望对DestroyFunction的任何调用都应该调用正确的虚拟函数。但是放置在Base析构函数调用的Base虚函数中的同一个Destroy函数。这与静态绑定(bind)或编译器优化有关吗?classBase{public:Base(){}voidDestroy(){callVirtual();}virtualvoidcallVirtual(){cout.cla

c++ - 构造函数中抛出的异常 : is the destructor called?

这个问题在这里已经有了答案:Whatdestructorsarerunwhentheconstructorthrowsanexception?(3个答案)关闭8年前。如果在对象的构造函数中抛出异常,那么是否会调用析构函数?还是未定义的行为?(这就是为什么我不愿意说出我的编译器做了什么。)structfoo(){foo(){throw"bar";}~foo(){/*amIcalled*/}};foof;