destructor

What is the use of having destructor as private?

别来无恙 提交于 2019-12-27 12:15:14
问题 What is the use of having destructor as private? 回答1: Basically, any time you want some other class to be responsible for the life cycle of your class' objects, or you have reason to prevent the destruction of an object, you can make the destructor private. For instance, if you're doing some sort of reference counting thing, you can have the object (or manager that has been "friend"ed) responsible for counting the number of references to itself and delete it when the number hits zero. A

What is the use of having destructor as private?

北城以北 提交于 2019-12-27 12:12:11
问题 What is the use of having destructor as private? 回答1: Basically, any time you want some other class to be responsible for the life cycle of your class' objects, or you have reason to prevent the destruction of an object, you can make the destructor private. For instance, if you're doing some sort of reference counting thing, you can have the object (or manager that has been "friend"ed) responsible for counting the number of references to itself and delete it when the number hits zero. A

calling destructor explicitly

[亡魂溺海] 提交于 2019-12-27 11:22:08
问题 I understand that in most cases, we should not call a destructor explicitly. However, I saw an example from C++11 Standard N3485 Section 13.4.5 Template arguments: An explicit destructor call for an object that has a type that is a class template specialization may explicitly specify the template-arguments. Example: template<class T> struct A { ~A(); }; void f(A<int>* p, A<int>* q) { p->A<int>::~A(); // OK: destructor call q->A<int>::~A<int>(); // OK: destructor call } It seems to me that we

C++ - destructors [closed]

走远了吗. 提交于 2019-12-25 21:09:10
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 8 years ago . In the external C++ learning resource that I'm reading. What is an example on this statement? When a variable goes out of scope, or a dynamically allocated variable is explicitly deleted using the delete keyword,

order of constructors and destructors

扶醉桌前 提交于 2019-12-25 18:53:29
问题 Here is a piece of code : #include<iostream> using namespace std; class cls { int x; public: cls(int i=0) {cout<<" c1 "; x=i;} ~cls() {cout<<" d 1 ";} }; class cls1 { int x; cls xx; public: cls1(int i=0){cout<<" c2 ";x=i;} ~cls1(){cout<<" d2 ";} }c; class cls2 { int x;cls1 xx;cls xxx; public: cls2(int i=0) {cout<<" c3 ";x=i;} ~cls2(){ cout<<" d3 ";} }; int main() { cls2 s; return 0; } The output is c1 c2 c1 c2 c1 c3 d3 d1 d2 d1 d2 d1 and I do not understand why . I would need some help. 回答1:

Destructor of List cannot delete the last node

久未见 提交于 2019-12-25 12:18:10
问题 Here is my test code: #include <iostream> #include <cstdlib> using namespace std; class List { private: class Node{ public: int data; Node* next; public: virtual ~Node() { if (next != NULL) { cout << "Node is out: " << data << endl; delete next; } } Node() { next = NULL; } }; Node* head; public: virtual ~List() { if (head != NULL) { delete head; } } List() { head = NULL; } public: void AddNode(int data); void DeleteNode(int data); //.... }; void List::AddNode(int data) { Node* temp = new Node

Using `__destruct` to implement default routing?

跟風遠走 提交于 2019-12-25 07:30:12
问题 I'm updating a PHP framework I've written. It used to just use a default behavior for routing. For example consider a case where the request goes to domain.com/package/controller/method ... $url = ["package", "controller", "method"]; //Check if package exists... //Check if controller exists in package... //Check if method exists in controller... This is all well and good, and works perfectly. However, I wanted to add some additional functionality to my router. That functionality being the

copy constructor,destructor and temporaries

痞子三分冷 提交于 2019-12-25 04:29:10
问题 I wrote this class to test the behaviour of the default constructor,the copy constructor, the assignment operator and the destructor: #include <iostream> class Test { public: Test(); Test(const Test&); ~Test(); Test &operator=(const Test&); private: static int count; int label; }; Test::Test() : label(count++) { std::cout<<"constructor of "<<label<<std::endl; } Test::Test(const Test &other) : label(count++) { std::cout<<"copy-constructor of "<<label<<std::endl; } Test::~Test() { std::cout<<

Lifetime of static class members/class references? [duplicate]

蓝咒 提交于 2019-12-25 02:43:08
问题 This question already has an answer here : Can a simple difference in Python3 variable names alter the way code runs? [duplicate] (1 answer) Closed 5 years ago . I was asked to show how to do a singleton like solution for the old chestnut of a special logger. At pains to point out the reasons for not doing this sort of thing, still, I tried. In doing so, I have a static class member disappearing unexpectedly. With this class declaration: epiLogger.py: import logging class epiLogger():

Why is the std::exception destructor not noexcept [duplicate]

早过忘川 提交于 2019-12-24 17:15:04
问题 This question already has an answer here : C++11 Exception's destructor allows to throw now? (1 answer) Closed last year . The destructor of the the C++11 std::exception base class is not noexcept , and thus may (in theory) throw an exception, which consequent relaxed permission for all its derived classes (including std::bad_alloc and std::runtime_error ). The destructor of the C++98 std::exception however had a throw() exception specification, indicating it was not permitted to throw