destructor

How do virtual destructors work?

纵饮孤独 提交于 2019-12-21 12:29:59
问题 Few hours back I was fiddling with a Memory Leak issue and it turned out that I really got some basic stuff about virtual destructors wrong! Let me put explain my class design. class Base { virtual push_elements() {} }; class Derived:public Base { vector<int> x; public: void push_elements(){ for(int i=0;i <5;i++) x.push_back(i); } }; void main() { Base* b = new Derived(); b->push_elements(); delete b; } The bounds checker tool reported a memory leak in the derived class vector. And I figured

Why is taking the address of a destructor forbidden?

半世苍凉 提交于 2019-12-21 06:52:34
问题 C++ standard at 12.4.2 states that [...] The address of a destructor shall not be taken. [...] However, one can without any complaints by the compiler take the address of a wrapper around a class destructor, like this: struct Test { ~Test(){}; void destructor(){ this->~Test(); } }; void (Test::*d)() = &Test::destructor; So what's the rationale behind forbidding to take the address of a destructor directly? 回答1: Constructors and destructors are somewhat special. The compiler often uses

Why is taking the address of a destructor forbidden?

那年仲夏 提交于 2019-12-21 06:52:18
问题 C++ standard at 12.4.2 states that [...] The address of a destructor shall not be taken. [...] However, one can without any complaints by the compiler take the address of a wrapper around a class destructor, like this: struct Test { ~Test(){}; void destructor(){ this->~Test(); } }; void (Test::*d)() = &Test::destructor; So what's the rationale behind forbidding to take the address of a destructor directly? 回答1: Constructors and destructors are somewhat special. The compiler often uses

Do trivial destructors cause aliasing

自作多情 提交于 2019-12-21 04:49:32
问题 C++11 §3.8.1 declares that, for an object with a trivial destructor, I can end its lifespan by assigning to its storage. I am wondering if trivial destructors can prolong the object's lifespan and cause aliasing woes by "destroying an object" that I ended the lifespan of much earlier. To start, something which I know is safe and alias-free void* mem = malloc(sizeof(int)); int* asInt = (int*)mem; *asInt = 1; // the object '1' is now alive, trivial constructor + assignment short* asShort =

Default constructor/destructor outside the class?

自古美人都是妖i 提交于 2019-12-21 03:37:34
问题 Is the following legal according to the C++11 standard ( = default outside the definition of the class) ? // In header file class Test { public: Test(); ~Test(); }; // In cpp file Test::Test() = default; Test::~Test() = default; 回答1: Yes, a special member function can be default-defined out-of-line in a .cpp file. Realize that by doing so, some of the properties of an inline-defaulted function will not apply to your class. For example, if your copy constructor is default-defined out-of-line,

Self destruction: this->MyClass::~MyClass() vs. this->~MyClass()

岁酱吖の 提交于 2019-12-21 03:11:22
问题 In my quest to learn C++ I stumbled across the article Writing Copy Constructors and Assignment Operators which proposes a mechanism to avoid code duplication across copy constructors and assignment operators. To summarise/duplicate the content of that link, the proposed mechanism is: struct UtilityClass { ... UtilityClass(UtilityClass const &rhs) : data_(new int(*rhs_.data_)) { // nothing left to do here } UtilityClass &operator=(UtilityClass const &rhs) { // // Leaves all the work to the

Destructors in Lua?

徘徊边缘 提交于 2019-12-20 19:11:46
问题 Is it possible to get destructors in Lua w/o using userdata? http://www.lua.org/notes/ltn006.html looks promising (in fact exactly what I want); except it's a path for Lua 4.0. Basically, I want a way to have a function called when a table is collected. Thanks! 回答1: From the documentation on metatables: A metatable may control how an object behaves in arithmetic operations, order comparisons, concatenation, length operation, and indexing. A metatable can also define a function to be called

How to deal with “exit-time destructor” warning in clang?

半城伤御伤魂 提交于 2019-12-20 16:13:10
问题 In my C++11 code I get the clang warning "Declaration requires an exit-time destructor" in the following case: static const std::map<int, const someStruct> mymap = { {1, { "A", "B", "C" }}, {2, { "D", "E", "F" }} }; As far as I understand Google an "exit-time destructor" is required to destroy main() and statics in a deterministic way to prevent crashes on exit due to "already released variables". Is that right? Can someone explain it better? Plus: What can I do about it (I don't want to

g++ __static_initialization_and_destruction_0(int, int) - what is it

大憨熊 提交于 2019-12-20 09:55:46
问题 After compiling of c++ file (with global static object) I get in nm output this function: 00000000 t _Z41__static_initialization_and_destruction_0ii __static_initialization_and_destruction_0(int, int) /* after c++filt */ What is it? It will call __cxa_atexit() Can I disable generation of this function (and calling a __cxa_atexit() ) and put all constructor and destructor calls to .ctors and .dtors sections? 回答1: This doc file seems to tell ya all you'd wanna know about those functions: http:/

C++ Correct way to free a vector of a custom class

孤街浪徒 提交于 2019-12-20 06:16:26
问题 I have my custom class, like: class MyClass { public: int i; std:string name; void DoSomeStuff(); } and another class with a list of my custom class: class MyClassList { public: std::vector<MyClasss> myClassList; } How shall be the list destructor in order to release all used vector space in memory: MyClassList::~MyClassList { myClassList.clear(); delete &myClassList; } Is that code right, redundant or wrong ? Thanks for helping... 回答1: You don't need to do anything, just let it fall out of