destructor

Is the __destruct method necessary for PHP?

Deadly 提交于 2019-11-27 22:18:06
问题 The manual said that The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence. Doesn't the PHP GC enough? Could someone give an example that __destruct method is necessary? 回答1: A destructor has nothing directly to do with releasing memory - instead it is a "hook" to allow custom code to be run when the object is eligible for reclamation. That is, it's the opposite of the

Why does destructor disable generation of implicit move methods?

不打扰是莪最后的温柔 提交于 2019-11-27 20:39:26
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 copies. In other words, adding a destructor to the class has caused presumably-efficient moves to be

What is the use of “delete this”?

我只是一个虾纸丫 提交于 2019-11-27 19:42:13
Today, I have seen some legacy code. In the destructor there is a statement like " delete this ". I think, this call will be recursive. Why it is working? I made some quick search on Y!, I found that if there is a need to restrict the user to create the stack object, we can make destructor private and provide an interface to delete the instance. In the interface provided, we have to call delete on this pointer. Are there any other situations for using such statements? JaredPar "delete this" is commonly used for ref counted objects. For a ref counted object the decision of when to delete is

C++ local variable destruction order

馋奶兔 提交于 2019-11-27 19:35:35
Is there a defined order in which local variables are deallocated in C++ (11) ? To be more concise: In which order will side effects of the destructors of two local variables in the same scope become visible? e.g.: struct X{ ~X(){/*do something*/} } int main(){ X x1; X x2; return 0; } Is x1 or x2 destroyed first when main returns or is the order undefined in C++11? James Kanze Within each category of storage classes (except dynamically allocated objects), objects are destructed in the reverse order of construction. I. About local variables Local variables are allocated on the Stack . The Stack

Destructors and noexcept

微笑、不失礼 提交于 2019-11-27 19:21:54
问题 I am a little bit confused with destructors and noexcept . My understanding was that in C++11 any destructor, including user-defined, is implicitly noexcept(true) , even if we throw from it. And one has to specify explicitly noexcept(false) if they want it to be that way for some reason. I'm seeing quite the opposite - with GCC 4.7.2, the user-defined destructor, no matter how primitive the class and destructor are, is implicitly noexcept(false) . What am I missing here? Is there some hidden

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

帅比萌擦擦* 提交于 2019-11-27 19:19:37
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 the most unlikely one foo() { if(static_cast<bar*>(this));//case 4 } Which of the above cases are

What's the difference between “= default” destructor and empty destructor?

喜你入骨 提交于 2019-11-27 19:10:37
问题 I want to prevent the user of my class from using it as an automatic variable, so I write code like this: class A { private: ~A() = default; }; int main() { A a; } I expect that the code won't be compiled, but g++ compiles it without error. However, when I change the code to: class A { private: ~A(){} }; int main() { A a; } Now, g++ gives the error that ~A() is private, as is my expectation. What's the difference between a "= default" destructor and an empty destructor? 回答1: Your first

Weird behaviour of C++ destructors

守給你的承諾、 提交于 2019-11-27 18:42:25
问题 #include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000, vector<int>(4, -1)); cout << dp.size(); } This tiny program takes a split second to execute when simply run from the command line. But when run in a debugger, it takes over 8 seconds. Pausing the debugger reveals that it is in the middle of destroying all those vectors. WTF? Note - Visual Studio 2008 SP1, Core 2 Duo 6700 CPU with 2GB of RAM. Added: To clarify, no, I'm not confusing Debug

Why is there no RAII in .NET?

坚强是说给别人听的谎言 提交于 2019-11-27 17:47:35
Being primarily a C++ developer the absence of RAII (Resource Acquisition Is Initialization) in Java and .NET has always bothered me. The fact that the onus of cleaning up is moved from the class writer to its consumer (by means of try finally or .NET's using construct ) seems to be markedly inferior. I see why in Java there is no support for RAII since all objects are located on the heap and the garbage collector inherently doesn't support deterministic destruction, but in .NET with the introduction of value-types ( struct ) we have the (seemingly) perfect candidate for RAII. A value type

C++ Destructors with Vectors, Pointers,

南笙酒味 提交于 2019-11-27 17:08:35
问题 As far as I know, I should destroy in destructors everything I created with new and close opened filestreams and other streams. However, I have some doubts about other objects in C++: std::vector and std::string s: Are they destroyed automatically? If I have something like std::vector<myClass*> of pointers to classes. What happens when the vector destructor is called? Would it call automatically the destructor of myClass ? Or only the vector is destroyed but all the Objects it contains are