destructor

Is using php destructor appropriate for displaying HTML?

瘦欲@ 提交于 2019-12-23 12:57:37
问题 If a class is implemented that builds HTML for a page by constructing it and calling various methods, is it appropriate to define the display/echo part of the class within the destructor? Instead of having a explicit Class:displayHTML(); method, having the echo $this->html in the destructor and whenever you are ready to display call unset($object); which would display it? I know the destructor probably is not the best place for this but wonder what others thoughts are on this? 回答1: That

Inheritance hierarchy: Constructor & Destructor execution sequence

大憨熊 提交于 2019-12-23 09:31:43
问题 Here http://www.parashift.com/c++-faq-lite/multiple-inheritance.html section [25.14] says The very first constructors to be executed are the virtual base classes anywhere in the hierarchy. I tried to verify it using following program: A (pure virtual) | B | C (virtual)/ \ (virtual) E D \ / F | G (pure virtual) | H each class has a c'tor and virtual d'tor. the output is as follows: A B C E D F G H ~H ~G ~F ~D ~E ~C ~B ~A Press any key to continue . . . but as per quote virtual base classes

Can i call destructor from its class method?

无人久伴 提交于 2019-12-23 08:56:23
问题 I have a Thread class like bellow class Thread { public: Thread(); ~Thread(); void start(); void stop(); } So i need to call destructor from stop() method, is it a good way to do that? 回答1: Yes delete this; But be careful. You should not use the deleted object, this and non-static members anymore. Another way is to call destructor ~Thread(); But the question is, why do you need call the destructor?! It's not logical. You can write a code to manage the resources in a private method and call it

Can i call destructor from its class method?

こ雲淡風輕ζ 提交于 2019-12-23 08:56:10
问题 I have a Thread class like bellow class Thread { public: Thread(); ~Thread(); void start(); void stop(); } So i need to call destructor from stop() method, is it a good way to do that? 回答1: Yes delete this; But be careful. You should not use the deleted object, this and non-static members anymore. Another way is to call destructor ~Thread(); But the question is, why do you need call the destructor?! It's not logical. You can write a code to manage the resources in a private method and call it

Deletion of pointer to incomplete type 'Point'; no destructor called

妖精的绣舞 提交于 2019-12-23 08:54:22
问题 I have 2 files: Point.h : class Point { int x; int y; char* name; public: Point() { name = new char[5]; } ~Point() { delete[] name; } }; and: Line.h : class Point; class Line { Point* p; public: Line() { p = new Point[2]; .... ... } ~Line() { delete[] p; } }; but when I compile, I got the next error: deletion of pointer to incomplete type 'Point'; no destructor called any help appreciated! 回答1: You need to add #include "Point.h" into your file Line.h . You can only construct and delete

Are there any instances when the destructor in PHP is NOT called?

懵懂的女人 提交于 2019-12-23 07:27:45
问题 This is my first time posting to stackoverflow, but I these threads have helped me tremendously! Anywho, onto my question... are there any instances when the destructor in PHP is NOT called? The reason I ask is because I have a mapper class which maps data to objects and in the constructor, I start a transaction and in the destructor I'll call a commit (I'll also have a member function which can also do the committal, if necessary). If there are any instances when the destructor isn't called,

C++ constructor & destructor order

给你一囗甜甜゛ 提交于 2019-12-23 07:26:54
问题 I am trying a code about base class and member construction and destruction and I am confused about some order of constuctor and destructor, the output of this code is: Base1 constructor Member1 constructor Member2 constructor Derived1 constructor Member3 constructor Member4 constructor Derived2 constructor Derived2 destructor Member4 destructor Member3 destructor Derived1 destructor Member2 destructor Member1 destructor Base1 destructor See the first four line, but I fell the order should be

Significance of trivial destruction

岁酱吖の 提交于 2019-12-23 06:57:14
问题 In C++17, the new std::optional mandates that it be trivially destructible if T is trivially destructible in [optional.object.dtor]: ~optional(); 1 Effects : If is_trivially_destructible_v<T> != true and *this contains a value, calls val->T::~T() . 2 Remarks : If is_trivially_destructible_v<T> == true then this destructor shall be a trivial destructor. So this potential implementation fragment would be non-conforming to the standard: template <class T> struct wrong_optional { union { T value;

C++ container which automatically deletes elements when destructed

断了今生、忘了曾经 提交于 2019-12-23 03:03:56
问题 I would like to create a list or map of references in C++ which automatically deletes its elements upon their destruction. Here is a structure demonstrating the idea. class Foo; class Bar { Foo foo; }; void main(void) { std::vector<Foo> foos; { Bar bar; foos.push_back(bar.foo); // foos[0] should be the same reference as bar.foo } // bar is now destructed // foos.size() should be 0 } There are two things wrong with my code. When push_back() is called with a std::vector, it creates a copy of

How should I write classes? C++

此生再无相见时 提交于 2019-12-23 01:46:22
问题 Hey.. I don't really get them. I read a tutorial about classes in C++, and I don't get a few things: In every example and tutorial that I've seen, functions are never written inside the class! For example, why write a class like this: #include <iostream> using namespace std; class test { private: int x, y; public: test (int, int); int tester () {return x + y; } }; test::test (int a, int b) { x = a; y = b; } int main() { test atest (3, 2); test atest2 (2, 6); cout << "test1: " << atest.tester(