destructor

C++ default destructor

柔情痞子 提交于 2019-12-17 21:44:46
问题 When I don't declare a constructor for example, the compiler will provide me with a default constructor that will have no arguments and no definition (body), and thus, will take no action . If I now don't declare a destructor , the compiler will provide me with a default destructor with no defintion (body), and thus, I think no action . So, if I'm finished with an object for example, wouldn't the default destructor reallocate (free) memory used by the object? If it doesn't, why are we getting

What happens to base class destructor if a derived class destructor throws an exception

点点圈 提交于 2019-12-17 18:17:32
问题 It just happened to me I wondered how resources are freed in the following case. class Base { Resource *r; public: Base() { /* ... */ } ~Base() { delete r; } }; class Derived : public Base { public: Derived() { /* ... */ } ~Derived() { /* Suddenly something here throws! */ } }; int main() { try { Derived d; } catch(...) { /* what happened with Base::r !? */ } } Will the base class destructor be called if the derived class destructor throws? Or will there be a leak? 回答1: According to §15.2/2:

Is it OK to use “delete this” to delete the current object?

随声附和 提交于 2019-12-17 16:49:31
问题 I'm writing a linked list and I want a struct's destructor (a Node struct) to simply delete itself, and not have any side effects. I want my list's destructor to iteratively call the Node destructor on itself (storing the next node temporarily), like this: //my list class has first and last pointers //and my nodes each have a pointer to the previous and next //node DoublyLinkedList::~DoublyLinkedList { Node *temp = first(); while (temp->next() != NULL) { delete temp; temp = temp->next(); } }

Is a Union Member's Destructor Called

心已入冬 提交于 2019-12-17 16:32:20
问题 C++11 allowed the use of standard layout types in a union : Member of Union has User-Defined Constructor My question then is: Am I guaranteed the custom destructor will be called, when the union goes out of scope? My understanding is that we must manually destroy and construct when switching: http://en.cppreference.com/w/cpp/language/union#Explanation But what about an example like this: { union S { string str; vector<int> vec; ~S() {} } s = { "Hello, world"s }; } When s goes out of scope,

How does an exception specification affect virtual destructor overriding?

那年仲夏 提交于 2019-12-17 16:07:11
问题 The C++ Standard states the following about virtual functions that have exception specifications: If a virtual function has an exception-specification , all declarations, including the definition, of any function that overrides that virtual function in any derived class shall only allow exceptions that are allowed by the exception-specification of the base class virtual function (C++03 §15.4/3). Thus, the following is ill-formed: struct B { virtual void f() throw() { } // allows no exceptions

Why does destructor disable generation of implicit move methods?

♀尐吖头ヾ 提交于 2019-12-17 15:56:10
问题 I was trying to understand what the rule of zero says by reading this blog. IMO, it says if you declare your own destructor then don't forget to make the move constructor and move assignment as default. Example: class Widget { public: ~Widget(); // temporary destructor ... // no copy or move functions }; "The addition of the destructor has the side effect of disabling generation of the move functions, but because Widget is copyable, all the code that used to generate moves will now generate

When is it safe to call this-> in constructor and destructor

谁说我不能喝 提交于 2019-12-17 15:32:34
问题 I've not been able to find a conclusive answer to this so far. When is it safe to call this-> from within an object. And in particular from inside the constructor and destructor. And also, when using public inheritance. Is it safe to use up and downcasting on the result of the this call? So for example: class foo { foo(): a(), b(this->a)//case 1 { this-> a = 5; //case 2 } int a; int b; }; class bar: public baz { bar(): baz(this)//case 3 - assuming baz has a valid constructor { } } And finally

Observable behavior and undefined behavior — What happens if I don't call a destructor?

只谈情不闲聊 提交于 2019-12-17 10:58:13
问题 Note: I've seen similar questions, but none of the answers are precise enough, so I'm asking this myself. This is a very nitpicky "language-lawyer" question; I'm looking for an authoritative answer. The C++ standard says: A program may end the lifetime of any object by reusing the storage which the object occupies or by explicitly calling the destructor for an object of a class type with a non-trivial destructor. For an object of a class type with a non-trivial destructor, the program is not

Under what circumstances are C++ destructors not going to be called?

狂风中的少年 提交于 2019-12-17 10:22:31
问题 I know that my destructors are called on normal unwind of stack and when exceptions are thrown, but not when exit() is called. Are there any other cases where my destructors are not going to get called? What about signals such as SIGINT or SIGSEGV? I presume that for SIGSEGV, they are not called, but for SIGNINT they are, how do I know which signals will unwind the stack? Are there any other circumstances where they will not be called? 回答1: Are there any other circumstances where they

Why destructor is not called on exception?

你离开我真会死。 提交于 2019-12-17 08:56:08
问题 I expected A::~A() to be called in this program, but it isn't: #include <iostream> struct A { ~A() { std::cout << "~A()" << std::endl; } }; void f() { A a; throw "spam"; } int main() { f(); } However, if I change last line to int main() try { f(); } catch (...) { throw; } then A::~A() is called. I am compiling with "Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86" from Visual Studio 2005. Command line is cl /EHa my.cpp . Is compiler right as usual? What does