destructor

继承中构造函数和析构函数的调用顺序

痴心易碎 提交于 2019-12-09 20:40:44
由架构中基类的设计想到的...... 现在,有三个类,类的定义如下 #include <iostream> using namespace std; class CA { public: CA(){ cout << "CA constructor" << endl; } ~CA(){ cout << "CA destructor" << endl; } }; class CB:public CA { public: CB(){ cout << "CB constructor" << endl; } ~CB(){ cout << "CB destructor" << endl; } }; class CC:public CB { public: CC(){ cout << "CC constructor" << endl; } ~CC(){ cout << "CC destructor" << endl; } }; int main(int argc, char* argv[]) { CC c; return 0; } CA 是爷爷, CB 是爸爸, CC 是儿子。 那么任何一本 C++ 的书都会讲,构造函数的调用顺序是 CA CB CC ,析构函数的调用顺序是 CC,CB,CA ,什么???你的书没讲,靠,扔了吧。 于是, 这个程序运行结果是 CA constructor CB

Stack unwinding in C++ when using Lua

倖福魔咒の 提交于 2019-12-09 16:45:11
问题 I recently stumbled into this this C++/Lua error int function_for_lua( lua_State* L ) { std::string s("Trouble coming!"); /* ... */ return luaL_error(L,"something went wrong"); } The error is that luaL_error use longjmp , so the stack is never unwound and s is never destructed, leaking memory. There are a few more Lua API's that fail to unwind the stack. One obvious solution is to compile Lua in C++ mode with exceptions. I, however, cannot as Luabind needs the standard C ABI. My current

static destructor

时间秒杀一切 提交于 2019-12-09 15:41:53
问题 Suppose I have: void foo() { static Bar bar; } Does c++ guarantee me that Bar::Bar() is called on bar, and Bar::~Bar() is never called on bar? (Until after main exits). Thanks! 回答1: Yes. The first time foo() is called, Bar bar will be constructed. It will then be available until main() finishes, after which point it will be destructed. It's essentially: static Bar *bar = 0; if (!bar) { bar = new Bar; // not "real", of course void delete_bar(void) { delete bar; } atexit(delete_bar); } Note I

C++ destructor & function call order

馋奶兔 提交于 2019-12-09 14:34:52
问题 Suppose I have the following snipplet: Foo foo; .... return bar(); Now, does the C++ standard guarantees me that bar() will be called before foo::~Foo() ? Or is this the compiler/implementation's choice? Thanks! 回答1: It is guaranteed behaviour. The actual execution is unrolled as follows: 0: enter block (scope) 1: Foo::Foo() 2. evaluation of bar(); as expression in return statement 3. save result of the expression as value returned from function 4. finalize return statement to leave function

Why does this virtual destructor trigger an unresolved external?

二次信任 提交于 2019-12-09 14:21:39
问题 Consider the following: In X.h: class X { X(); virtual ~X(); }; X.cpp: #include "X.h" X::X() {} Try to build this (I'm using a .dll target to avoid an error on the missing main, and I'm using Visual Studio 2010): Error 1 error LNK2001: unresolved external symbol "private: virtual __thiscall X::~X(void)" (??1X@@EAE@XZ) Small modifications result in a successful build, however: X.h: class X { inline X(); // Now inlined, and everything builds virtual ~X(); }; or X.h: class X { X(); ~X(); // No

Destructor parameters

可紊 提交于 2019-12-09 14:00:31
问题 The article Are destructors overloadable? talks about overloading the destructor. This raised a question: Can a destructor have parameters? I've never used or seen a destructor with parameters. I could not come up with an example of a reason to use parameters to the destructor. 回答1: Section §12.4 of C++0x draft n3290 has this to say about destructors: Destructors A special declarator syntax using an optional function-specifier (7.1.2) followed by ˜ followed by the destructor’s class name

Qt destructor call for closed widget

被刻印的时光 ゝ 提交于 2019-12-09 10:18:48
问题 There is application that handles text commands. I have a Qt widget that is closed with some close * command. Qt::WA_DeleteOnClose attribute is set for that widget, it receives closeEvent, but destructor for that object is called later (I guess on idle). If I have two commands close *; get something; the program crashes because get something is called before destructor for that widget, so it tries to access data deleted by close * command. How can I force Qt to call destructors?

Undefined reference to 'operator delete(void*)'

a 夏天 提交于 2019-12-09 08:21:35
问题 I'm new to C++ programming, but have been working in C and Java for a long time. I'm trying to do an interface-like hierarchy in some serial protocol I'm working on, and keep getting the error: Undefined reference to 'operator delete(void*)' The (simplified) code follows below: PacketWriter.h: class PacketWriter { public: virtual ~PacketWriter() {} virtual uint8_t nextByte() = 0; } StringWriter.h: class StringWriter : public PacketWriter { public: StringWriter(const char* message); virtual

order of destruction using virtual

依然范特西╮ 提交于 2019-12-09 05:56:32
问题 Can some one please help what the order of destruction is when I am using virtual functions. Does it start with the base class and then derived class? 回答1: Since I don't see how virtual function change any objects' destruction order, I assume you're referring to the order of destruction for base classes and data members in a virtual inheritance scenario. Sub-objects are constructed base classes are constructed from most base to most derived ; multiple base classes are constructed in the order

Why is a destructor called if it's deleted and not called if it's not deleted?

泄露秘密 提交于 2019-12-09 05:01:28
问题 Consider the following code: #include <iostream> struct A { A(){ }; ~A(){ std::cout << "~A::A()" << std::endl; }; }; struct B: A { }; B *b = new B; //Doesn't produce any side-effect. int main(){ } DEMO The program doesn't produce any output which means the destructor isn't being called. But if we replace the destructor's body with the delete specifier, the program won't even compile. #include <iostream> struct A { A(){ }; ~A() = delete; //{ std::cout << "~A::A()" << std::endl; }; }; struct B: