destructor

What determines when a class object is destroyed in PHP?

筅森魡賤 提交于 2019-11-29 01:30:27
Let's say that we have class CFoo . In the following example when is CFoo::__destruct() called? function MyPHPFunc() { $foo = new CFoo(); . . . // When/where/how does $foo get destroyed/deleted? } In this example would the destructor be called when the script exits the scope of MyPHPFunc because $foo would no longer be accessible? In PHP all values are saved in so called zval s. Those zval s contain the actual data, type information and - this is important for your question - a reference count. Have a look at the following snippet: $a = new B; // $a points to zval(new B) with refcount=1 $b =

Does std::vector call the destructor of pointers to objects? [duplicate]

穿精又带淫゛_ 提交于 2019-11-28 23:04:58
Possible Duplicate: Deleting pointers in a vector I know when an std::vector is destructed, it will call the destructor of each of its items. Does it call the destructor of pointers to objects? vector<myclass*> stuff; When stuff is destroyed, do the individual objects pointed to by the pointers inside stuff get destructed? No. How is std::vector supposed to know how to destroy the pointed-to object? Should it use delete ? delete[] ? free ? Some other function? How is it supposed to know the pointed-to objects are actually dynamically allocated or that it is the One True Owner and is

Are destructors run when calling exit()? [duplicate]

China☆狼群 提交于 2019-11-28 22:49:04
Possible Duplicate: Will exit() or an exception prevent an end-of-scope destructor from being called? In C++, when the application calls exit(3) are the destructors on the stack supposed to be run to unwind the stack? Cheers and hth. - Alf No, most destructors are not run on exit() . C++98 §18.3/8 discusses this. Essentially, when exit is called static objects are destroyed, atexit handlers are executed, open C streams are flushed and closed, and files created by tmpfile are removed. Local automatic objects are not destroyed. I.e., no stack unwinding. Calling abort lets even less happen: no

“delete this” in constructor

十年热恋 提交于 2019-11-28 22:48:21
What actually happen when I execute this code? class MyClass { MyClass() { //do something delete this; } } It turns out that in this particular case the code is legal, but you're ε-away from undefined behavior. The C++ standard defines the notion of the "lifetime" of an object to be the time between which its constructor has finished running and when the destructor starts running. It also explicitly states (in §3.8/5) that Before the lifetime of an object has started [...] If the object will be or was of a class type with a non-trivial destructor, and the pointer is used as the operand of a

How to add constructors/destructors to an unnamed class?

痞子三分冷 提交于 2019-11-28 19:08:30
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? Vlad from Moscow You can not declare a constructor or destructor for an unnamed class because the constructor and destructor names need to match the class name. In your example, the unnamed class is local. It has no

How to write a simple class in C++?

萝らか妹 提交于 2019-11-28 18:34:22
I have been reading a lot of tutorials on C++ class but they miss something that other tutorials include. Can someone please show me how to write and use a very simple C++ class that uses visibility, methods and a simple constructor and destructor? TStamper Well documented example taken and explained better from Constructors and Destructors in C++ : #include <iostream> // for cout and cin class Cat // begin declaration of the class { public: // begin public section Cat(int initialAge); // constructor Cat(const Cat& copy_from); //copy constructor Cat& operator=(const Cat& copy_from); //copy

Why do we need to use virtual ~A() = default; instead of virtual ~A() {} in C++11?

家住魔仙堡 提交于 2019-11-28 17:21:01
问题 In Stack Overflow post Checking the object type in C++11 , I have the comment: In C++11 you'll actually want to do virtual ~A() = default; Otherwise, you'll lose the implict move constructors. What is virtual ~A() = default; for? How come implicit move constructors lost with virtual ~A() {} ? 回答1: The comment is not correct. Both: virtual ~A() = default; and virtual ~A() {} are user declared . And the implicit move members are inhibited if the destructor is user declared. [dcl.fct.def.default

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

怎甘沉沦 提交于 2019-11-28 16:56:49
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); } 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" function doesn't necessarily mean the function will be inlined. That depends on the complexity of the function

C++ default destructor

孤街浪徒 提交于 2019-11-28 16:39:17
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 it? And, maybe the same question applies to the default constructor . If it doesn nothing, why is it

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

℡╲_俬逩灬. 提交于 2019-11-28 14:20:27
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? void Dowork(CTemp obj) Here local-copy will be done, that will be destruct after exit from scope of DoWork function, that's why you see destructor-call. Implement a copy constructor and check again: