destructor

php destructor behaviour

走远了吗. 提交于 2019-11-29 12:46:19
问题 im trying to understand php constructor and destructor behaviour. Everything goes as expected with the constructor but i am having trouble getting the destructor to fire implicitly. Ive done all the reading on php.net and related sites, but i cant find an answer to this question. If i have a simple class, something like: class test{ public function __construct(){ print "contructing<br>"; } public function __destruct(){ print "destroying<br>"; } } and i call it with something like: $t = new

Writing a LinkedList destructor?

笑着哭i 提交于 2019-11-29 12:06:15
问题 Is this a valid LinkedList destructor? I'm still sort of confused by them. I want to make sure I'm understanding this correctly. LinkedList::~LinkedList() { ListNode *ptr; for (ptr = head; head; ptr = head) { head = head->next delete ptr; } } So at the beginning of the loop, pointer ptr is set to hold the address of head, the first node in the list. head is then set to the next item, which will become the beginning of the list once this first deletion takes place. ptr is deleted, and so is

Destruction of return value on destructor exception

孤者浪人 提交于 2019-11-29 10:37:17
问题 I have the following code: #include <stdexcept> #include <iostream> struct ok { int _n; ok(int n) : _n(n) { std::cerr << "OK" << n << " born" << std::endl; } ~ok() { std::cerr << "OK" << _n << " gone" << std::endl; } }; struct problematic { ~problematic() noexcept(false) { throw std::logic_error("d-tor exception"); } }; ok boo() { ok ok1{1}; problematic p; ok ok2{2}; return ok{3}; // Only constructor is called... } int main(int argc, char **argv) { try {boo();} catch(...) {} } I see that he

Uses of destructor = delete;

泄露秘密 提交于 2019-11-29 10:34:02
问题 Consider the following class: struct S { ~S() = delete; }; Shortly and for the purpose of the question: I cannot create instances of S like S s{}; for I could not destroy them. As mentioned in the comments, I can still create an instance by doing S *s = new S; , but I cannot delete it as well. Therefore, the only use I can see for a deleted destructor is something like this: struct S { ~S() = delete; static void f() { } }; int main() { S::f(); } That is, define a class that exposes only a

How to write destructor for union-like class

心已入冬 提交于 2019-11-29 10:24:40
I'm trying to use an union (C++) that has some non-primitive variables, but I'm stuck trying to create the destructor for that class. As I have read, it is not possible to guess what variable of the union is being used so there is no implicit destructor, and as I'm using this union on the stack, the compiler errors that the destructor is deleted. The union is as follows: struct LuaVariant { LuaVariant() : type(VARIANT_NONE) { } LuaVariantType_t type; union { std::string text; Position pos; uint32_t number; }; }; The type variable holds what field of the union is being used (chosen from an enum

C++:When creating a new objects inside a function and returning it as result, must I use the new operator to create the object?

心已入冬 提交于 2019-11-29 09:25:49
问题 I have two dummy questions which have confused me for a while. I did do some searching online and read through much c++ tutorials, however I cannot find concrete answers. Say we have a class named Node which is a building block of a singly linked list. class Node { int data; Node* next; } Fact1: local variables(non static) will be destroyed upon the exit of the corresponding functions. Question1: How about the situations blow: Node* func() { Node n; Node* ptr=&n; return n; } Will the node n

Are destructors overloadable?

醉酒当歌 提交于 2019-11-29 09:24:20
enable_if doc page says: Constructors and destructors do not have a return type; an extra argument is the only option. Are destructors overloadable? Prasoon Saurav No Are destructors overloadable? The answer is plain No . Two versions of desturctor cannot co-exist in a class body. However unlike the popular belief, note that destructor does have 2 syntax. struct E { ~E(); // syntax-1 ~E() throw(); // syntax-2 }; Syntax-2 is less popular. But it is mandatory, if the base class destructor contains similar syntax. The best example is inheriting std::exception . Note that, not complying to such

Javascript's equivalent of destruct in object model [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-11-29 09:02:51
This question already has an answer here: When are JavaScript objects destroyed? 4 answers Since I've dealt in the past with javascript's funky "object model", I assume there is no such thing as a destructor. My searches were mildly unsuccessful, so you guys are my last well of hope. How do you execute stuff upon instance destruction? MDN is a nice resource for JS. No, there is nothing like calling a function when an object ceases. 来源: https://stackoverflow.com/questions/22566307/javascripts-equivalent-of-destruct-in-object-model

Delete calling destructor but not deleting object?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 08:49:41
So I have been working with c++ and pointers for a year and a half now, and i thought i had them succeed. I have called delete on objects many times before and the objects actually got deleted, or at least i thought they did. The code below is just confusing the hell out of me: #include <iostream> class MyClass { public: int a; MyClass() : a(10) { std::cout << "constructor ran\n"; } void method(std::string input_) { std::cout << param_ << "\n"; } ~MyClass() { std::cout << "destructor ran\n"; } }; int main() { MyClass* ptr = new MyClass; ptr->method("1"); delete ptr; ptr->method("2.5"); } this