草庐IT

Destructor

全部标签

c++ - C++ 是否为纯虚拟类创建默认值 "Constructor/Destructor/Copy Constructor/Copy assignment operator"?

C++编译器是否为这个“类”生成默认函数,如Constructor/Destructor/Copy-Constructor...?classIMyInterface{virtualvoidMyInterfaceFunction()=0;}我的意思是不可能实例化这个“类”,所以我认为没有生成默认函数。否则,人们会说您必须使用虚拟析构函数。这意味着如果我不定义析构函数virtual它将默认创建,而不是virtual。此外,我想知道为纯虚拟接口(interface)定义虚拟析构函数是否合理,如上所示?(所以这里没有使用指针或数据,所以不需要破坏任何东西)谢谢。 最

c++ - "temporary of type ' A ' has protected destructor", 但它的类型是 B

在以下代码中,使用Clang8.0.0+和-std=c++17编译,使用B{}创建派生类实例会报错错误:'A'类型的临时对象具有protected析构函数。当临时文件的类型为B(因此应该有一个公共(public)析构函数)时,为什么A会出现在此消息中?https://godbolt.org/z/uOzwYaclassA{protected:A()=default;~A()=default;};classB:publicA{//canalsoomitthese3lineswiththesameresultpublic:B()=default;~B()=default;};voidfoo(

c++ - VS2015 更新 1 个错误,或错误的 C++ : Why can't a friend class access its friend's protected destructor?

以下似乎是ZeroCICE在其自动生成的代码中采用的一种模式,在我看来,这似乎是他们现在为其工具的许多版本制作单例(不确定为什么)的一种方式。各种编译器都没有问题,直到今天发现VisualStudio2015Update1(VS版本14.0.24720.00,VC++版本19.00.23506)报错。在Update1之前,VS2015也没有问题。我不确定它是带有Update1的VS2015C++编译器中的错误(回归?),还是其他编译器放任自流的错误(不符合标准)C++代码。这是代码模式的示例:classFoo{protected:virtual~Foo(){}friendclassFo

c++ - 有没有办法调用纯虚类的 "deleting destructor"?

我在UbuntuTrusty上使用C++11和g++4.8。考虑这个片段classParent{public:virtual~Parent()=default;virtualvoidf()=0;};classChild:publicParent{public:voidf(){}};使用调用{Childo;o.f();}{Parent*o=newChild;deleteo;}{Child*o=newChild;deleteo;}我使用gcov生成我的代码覆盖率报告。它报告从未调用带有符号_ZN6ParentD0Ev的析构函数,而_ZN6ParentD2Ev是。回答Dualemission

c++ - std::is_constructible on type with non-public destructor

std::is_constructible的预期结果是什么?在具有私有(private)或protected析构函数的类型上?例如,即使只有friend可以释放它,我仍然可以在堆上构造这样一个对象:#includeclassFoo{friendvoidfreeFoo(Foo*);public:Foo(){}private://Destructorisprivate!~Foo(){}};voidfreeFoo(Foo*f){deletef;//deletingafooisfineherebecauseoffriendship}intmain(){Foo*f=newFoo();//dele

kotlin - Kotlin 编程语言中的析构函数

我是Kotlin新手,在kotlin中写了一个类来执行数据库操作我已经使用init在构造函数中定义了数据库连接,但我想使用析构函数关闭数据库连接。知道如何使用kotlin析构函数来实现这一点吗?目前我已经编写了一个单独的函数来关闭连接,我希望它像php等任何其他编程语言一样使用析构函数 最佳答案 在Kotlin中处理需要关闭的资源您可以使您的数据库包装器扩展Closeable.然后你可以像这样使用它。valresult=MyResource().use{resource->resource.doThing();}这样在usebloc

kotlin - Kotlin 编程语言中的析构函数

我是Kotlin新手,在kotlin中写了一个类来执行数据库操作我已经使用init在构造函数中定义了数据库连接,但我想使用析构函数关闭数据库连接。知道如何使用kotlin析构函数来实现这一点吗?目前我已经编写了一个单独的函数来关闭连接,我希望它像php等任何其他编程语言一样使用析构函数 最佳答案 在Kotlin中处理需要关闭的资源您可以使您的数据库包装器扩展Closeable.然后你可以像这样使用它。valresult=MyResource().use{resource->resource.doThing();}这样在usebloc

c++ - 好坏 : Calling destructor in constructor

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。ImprovethisquestionBreak:我不认为这实际上是同一个问题,另一个问题是关于手动调用析构函数的一般问题。这是在类本身内部的创建过程中。仍然想知道执行此操作时会发生什么,如下面的问题所述。起初,我认为它很糟糕,真的很糟糕。只是分析构造函数的这段代码(见下文),由两个人编写,需要将其转换为Delphi对象Pascal。它的行为必须与C版本相同。我不喜欢这种风格,非常丑陋,但没关系。另一件事,在代码的两个阶段,它

swift - 无法为类型 'sqlite3_destructor_type' 调用初始值设定项

到目前为止answerfromMartinR已经完美地工作了。但是从Swift2开始,它现在会引发错误Cannotinvokeinitializerfortype'sqlite3_destructor_type'withanargumentlistoftype'(COpaquePointer)'在那些行中:privateletSQLITE_STATIC=sqlite3_destructor_type(COpaquePointer(bitPattern:0))//https://stackoverflow.com/a/26884081/1271826privateletSQLITE_TR

c++ - 类 : handling copy constructor and destructor (C++) 内的 vector

以“big3”(构造函数、复制构造函数、析构函数)的简单类:#includeusingnamespacestd;//actuallygoesintheCfilethatlinkstothisheaderfile...classplanets(){//storesmassandradiidataforplanetsinasolarsystem.public:vectormass;vectorradius;//constructorplanets(intnumObj){for(inti=0;imass(p.mass);//copyvectorsintonewclass.vectorradi