destructor

Why doesn't the C++ default destructor destroy my objects?

*爱你&永不变心* 提交于 2019-12-03 09:09:00
问题 The C++ specification says the default destructor deletes all non-static members. Nevertheless, I can't manage to achieve that. I have this: class N { public: ~N() { std::cout << "Destroying object of type N"; } }; class M { public: M() { n = new N; } // ~M() { //this should happen by default // delete n; // } private: N* n; }; Then this should print the given message, but it doesn't: M* m = new M(); delete m; //this should invoke the default destructor 回答1: What makes you think the object n

Why must a base class destructor be accessible only when a custom constructor is declared?

£可爱£侵袭症+ 提交于 2019-12-03 08:54:00
问题 Comeau, g++ (ideone) and EDG accept the following code without diagnostic. Visual C++ compiles successfully, albeit with warning C4624. class indestructible_base { ~indestructible_base(); }; class T : indestructible_base { public: //T() {} }; int main(void) { new T(); } Uncomment the constructor and it no longer compiles. Perhaps it's the rule that if an exception occurs inside the constructor, subobjects must be destroyed? Seems odd, since the body is empty and can't cause an exception. Even

How can I handle interrupt signal and call destructor in c++? [duplicate]

廉价感情. 提交于 2019-12-03 07:40:37
Possible Duplicate: Is destructor called if SIGINT or SIGSTP issued? My code like this: #include <iostream> #include <signal.h> #include <cstdlib> void handler(int) { std::cout << "will exit..." << std::endl; exit(0); } class A { public: A() {std::cout << "constructor" << std::endl;} ~A() {std::cout << "destructor" << std::endl;} }; int main(void) { signal(SIGINT, &handler); A a; for (;;); return 0; } When I pressed Ctrl-C, it printed: constructor ^Cwill exit... There is no "destructor" printed. So, how can I exit cleanly? With difficulty. Already, the code you've written has undefined

delete a NULL pointer does not call overloaded delete when destructor is written

好久不见. 提交于 2019-12-03 07:10:14
问题 class Widget { public: Widget() { cout<<"~Widget()"<<endl; } ~Widget() { cout<<"~Widget()"<<endl; } void* operator new(size_t sz) throw(bad_alloc) { cout<<"operator new"<<endl; throw bad_alloc(); } void operator delete(void *v) { cout<<"operator delete"<<endl; } }; int main() { Widget* w = 0; try { w = new Widget(); } catch(bad_alloc) { cout<<"Out of Memory"<<endl; } delete w; getch(); return 1; } In this code, delete w does not call the overloaded delete operator when the destructor is there

order of destruction using virtual

十年热恋 提交于 2019-12-03 07:08:14
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? 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 of their declaration as base classes ; virtual base classes are constructed before all others , amongst

Time complexity of delete[] operator [duplicate]

独自空忆成欢 提交于 2019-12-03 06:30:22
问题 This question already has answers here : Is Big-O of the C++ statement 'delete [] Q;' O(1) or O(n)? (2 answers) Closed 5 years ago . What is the Time Complexity of the delete[] operator? I mean how is it implemented - does it iterate over all the elements in the array and calls destructor for every element? Does this operator do the same for primitive types ( int , etc.) and user defined types? 回答1: ::operator delete[] is documented on cplusplus.com (which is sometimes frowned upon) as:

Destructors in Lua?

烂漫一生 提交于 2019-12-03 06:06:30
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! 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 when a userdata is garbage collected. The Lua Users' Lua FAQ states: Why doesn't the __gc and __len

Why are inline constructors and destructors not a good idea in C++?

大城市里の小女人 提交于 2019-12-03 05:54:55
问题 I remember reading in one of the C++ books (quite some time ago) that it is not a good idea to have inline Constructors and Destructors especially for derived class. I understand that inlining would induce some bloating up of object code but are there any other design considerations that discourage inline constructors and destructors? Of course most compilers may reject the inline and proceed with creating a function body but if they were to inline what penalty might one have to pay? 回答1: The

Properly destroying pointers in an std::map

限于喜欢 提交于 2019-12-03 05:36:46
问题 I have a map declared as std::map<std::string, Texture*> textureMap; which I use for pairing the path to a texture file to the actual texture so I can reference the texture by the path without loading the same texture a bunch of times for individual sprites. What I don't know how to do is properly destroy the textures in the destructor for the ResourceManager class (where the map is). I thought about using a loop with an iterator like this: ResourceManager::~ResourceManager() { for(std::map

Use cases for __del__

点点圈 提交于 2019-12-03 05:09:01
问题 What are use cases in python 3 of writing a custom __del__ method or relying on one from stdlib 1 ? That is, in what scenario is it reasonably safe, and can do something that's hard to do without it? For many good reasons (1 2 3 4 5 6), the usual recommendation is to avoid __del__ and instead use context managers or perform the cleanup manually: __del__ is not guaranteed to be called if objects are alive on intrepreter exit 2 . At the point one expects the object can be destroyed, the ref