destructor

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

自古美人都是妖i 提交于 2019-11-27 13:57:06
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 required to call the destructor explicitly before the storage which the object occupies is reused or

How to add constructors/destructors to an unnamed class?

折月煮酒 提交于 2019-11-27 11:55:59
问题 Is there a way to declare a constructor or a destructor in an unnamed class? Consider the following void f() { struct { // some implementation } inst1, inst2; // f implementation - usage of instances } Follow up question : The instances are ofcourse constructed (and destroyed) as any stack based object. What gets called? Is it a mangled name automatically assigned by the compiler? 回答1: You can not declare a constructor or destructor for an unnamed class because the constructor and destructor

What destructors are run when the constructor throws an exception?

删除回忆录丶 提交于 2019-11-27 11:10:40
In C++, if a constructor throws an exception, what destructors are run? In particular, does it make any difference if the exception is during the initialization list or the body? Also, what about inheritance and members? Presumably all completed constructions get destructed. If only some members are constructed, do only those get destructed? If there is multiple inheritance, do all completed constructors get destructed? Does virtual inheritance change anything? if a constructor throws an exception, what destructors are run? Destructors of all the objects completely created in that scope. Does

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

筅森魡賤 提交于 2019-11-27 10:52:08
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? stinky472 Are there any other circumstances where they[destructors] will not be called? Long jumps: these interfere with the natural stack unwinding process and

C++ Constructor/Destructor inheritance

让人想犯罪 __ 提交于 2019-11-27 10:27:52
EDIT : Summary of answers In the following, B is a subclass of A. It's a matter of terminology; ctors and dtors are not inherited, in the sense that the ctor/dtor of B will not be borrowed from A's interface. A class has at least one constructor, and has exactly one destructor. Constructors : B does not inherit constructors from A; Unless B's ctor explicitely calls one of A's ctor, the default ctor from A will be called automatically before B's ctor body (the idea being that A needs to be initialized before B gets created). Destructors : B does not inherit A's dtor; After it exits, B's

In C++ can constructor and destructor be inline functions?

主宰稳场 提交于 2019-11-27 10:08:09
问题 VC++ makes functions which are implemented within the class declaration inline functions. If I declare a class Foo as follows, then are the CONSTRUCTOR and DESTRUCTOR inline functions? class Foo { int* p; public: Foo() { p = new char[0x00100000]; } ~Foo() { delete [] p; } }; { Foo f; (f); } 回答1: Defining the body of the constructor INSIDE the class has the same effect of placing the function OUTSIDE the class with the "inline" keyword. In both cases it's a hint to the compiler. An "inline"

Why is the destructor getting called twice but the constructor only once?

梦想的初衷 提交于 2019-11-27 08:38:11
问题 My code is class CTemp{ public: CTemp(){ printf("\nIn cons"); } ~CTemp(){ printf("\nIn dest"); } }; void Dowork(CTemp obj) { printf("\nDo work"); } int main() { CTemp * obj = new CTemp(); Dowork(*obj); delete obj; return 0; } The output that I get is In cons Do work In dest In dest Now why does the constructor get called once but the destructor is called twice? Can someone please explain this? 回答1: void Dowork(CTemp obj) Here local-copy will be done, that will be destruct after exit from

Why, really, deleting an incomplete type is undefined behaviour?

帅比萌擦擦* 提交于 2019-11-27 08:16:05
Consider this classic example used to explain what not to do with forward declarations: //in Handle.h file class Body; class Handle { public: Handle(); ~Handle() {delete impl_;} //.... private: Body *impl_; }; //--------------------------------------- //in Handle.cpp file #include "Handle.h" class Body { //Non-trivial destructor here public: ~Body () {//Do a lot of things...} }; Handle::Handle () : impl_(new Body) {} //--------------------------------------- //in Handle_user.cpp client code: #include "Handle.h" //... in some function... { Handle handleObj; //Do smtg with handleObj... /

After an object is destroyed, what happens to subobjects of scalar type?

谁说我不能喝 提交于 2019-11-27 08:07:37
问题 Consider this code (for different values of renew and cleanse ): struct T { int mem; T() { } ~T() { mem = 42; } }; // identity functions, // but breaks any connexion between input and output int &cleanse_ref(int &r) { int *volatile pv = &r; // could also use cin/cout here return *pv; } void foo () { T t; int &ref = t.mem; int &ref2 = cleanse ? cleanse_ref(ref) : ref; t.~T(); if (renew) new (&t) T; assert(ref2 == 42); exit(0); } Is the assert guaranteed to pass? I understand that this style is

Ruby: Destructors?

守給你的承諾、 提交于 2019-11-27 07:36:36
I need to occasionaly create images with rmagick in a cache dir. To then get rid of them fast, without loosing them for the view, I want to delete the image-files while my Ruby Instance of the Image-Class get's destructed or enters the Garbage Collection. What ClassMethod must I overwrite to feed the destructor with code? You can use ObjectSpace.define_finalizer when you create the image file, and it will get invoked when the garbage man comes to collect. Just be careful not to reference the object itself in your proc, otherwise it won't be collected by the garbage man. (Won't pick up