destructor

Can the default destructor be generated as a virtual destructor automatically?

穿精又带淫゛_ 提交于 2019-12-02 17:23:29
Can the default destructor be generated as a virtual destructor automatically? If I define a base class but no default destructor, is there a default virtual destructor generated automatically? No. There is a cost associated with making a method virtual, and C++ has a philosophy of not making you pay for things that you don't explicitly state that you want to use. If a virtual destructor would have been generated automatically, you would have been paying the price automatically. Why not just define an empty virtual destructor? In C++ 11 you can use: class MyClass { // create a virtual, default

Is it a good idea to shut down a class's thread member in the class's destructor?

安稳与你 提交于 2019-12-02 14:15:36
What's the best way to shut down a Boost thread managed by a C++ class when it's time for an object of that class to be destroyed? I have a class which creates and starts a thread on construction and provides a public Wake() method which wakes the thread when it's time to do some work. The Wake() method uses a Boost mutex and a Boost condition variable to signal the thread; the thread procedure waits on the condition variable, then does the work and goes back to waiting. At the moment, I shut this thread down in the class's destructor, using a boolean member variable as a "running" flag; I

RAII vs. exceptions

扶醉桌前 提交于 2019-12-02 14:04:26
The more we use RAII in C++, the more we find ourselves with destructors that do non-trivial deallocation. Now, deallocation (finalization, however you want to call it) can fail, in which case exceptions are really the only way to let anybody upstairs know of our deallocation problem. But then again, throwing-destructors are a bad idea because of the possibility of exceptions being thrown during stack unwinding. std::uncaught_exception() lets you know when that happens, but not much more, so aside from letting you log a message before termination there's not much you can do, unless you're

What happens if you call a constructor from a destructor?

依然范特西╮ 提交于 2019-12-02 13:34:58
Calling __construct() function from __destruct(), <?php public function __construct() { echo "Hi"; } public function __destruct() { $this->__construct(); } ?> will it create infinite loop? No, but this will: class Test { public function __construct() { echo "Hi"; } public function __destruct() { new Test(); } } new Test(); Example: http://ideone.com/94XUg No, it won't. __construct is just regular function while called directly instead of using new ClassName; 来源: https://stackoverflow.com/questions/10384836/what-happens-if-you-call-a-constructor-from-a-destructor

C++ Correct way to free a vector of a custom class

痴心易碎 提交于 2019-12-02 11:54:43
I have my custom class, like: class MyClass { public: int i; std:string name; void DoSomeStuff(); } and another class with a list of my custom class: class MyClassList { public: std::vector<MyClasss> myClassList; } How shall be the list destructor in order to release all used vector space in memory: MyClassList::~MyClassList { myClassList.clear(); delete &myClassList; } Is that code right, redundant or wrong ? Thanks for helping... You don't need to do anything, just let it fall out of scope. RAII will ensure that the vector memory is cleaned up when your instance of MyClassList falls out of

Why is my destructor called twice?

☆樱花仙子☆ 提交于 2019-12-02 09:18:19
问题 Suppose I hace a class Student with the method: Student Student::method(Student x) { //nothing important return x; } The copy constructor is called twice, once when the object x is send as a parameter and second when x is returned from the function. Why and when is the destructor for class Student called twice when I call this method? The call is like this: a = b.method(c), where a, b and c are Student objects. 回答1: For your example, a = b.method(c); , there are three copies that may take

Debug assertion failed BLOCK_TYPE_IS_VALID(pHead->nblockuse) from Deconstructor

十年热恋 提交于 2019-12-02 09:08:34
I am quite lost right now. I made a vector class. Everything works how I would like it to work, until the end. When the destructor is called I get an error message: Debug assertion failed BLOCK_TYPE_IS_VALID(pHead->nblockuse). I've seen quite a few questions like this one on SO, but what I have tried hasn't worked. part of .h. private: int* _myArray; int _size; int _capacity; #include "File.h" const string RETURN_CARRIAGE_STR = "\n"; const string SIZE_STR = "Size "; const string CAPACITY_STR = "Capacity "; const int INITIAL_CAPACITY = 2; int main(void) { cout << "\nCreating a vector Sam of

how to achive - file write open on __del__?

允我心安 提交于 2019-12-02 08:53:32
问题 I m trying to do a some activity on class obj destruction. How do I achive file open in _del__ function? (I m using Python 3.4) class iam(object): def __init__(self): print("I m born") def __del__(self): f = open("memory_report.txt", "w") f.write("He gone safe") f.close() if __name__ == '__main__': i = iam() print("Script Ends. Now to GC clean memory") Output: I m born Script Ends. Now to GC clean memory Exception ignored in: <bound method iam.__del__ of <__main__.iam object at

Destructor class in TObject and NIL Delphi

醉酒当歌 提交于 2019-12-02 08:18:36
I am wondering why after I invoke Free method the object is not nil . What I mean for example next class: type Ta = class(TObject) public i: integer; destructor Destroy; override; end; destructor Ta.Destroy; begin inherited; end; procedure Form1.Button1; var a: Ta; begin a := Ta.Create; a.Free; if a = nil then button1.Caption := 'is assigned' else button1.caption := 'is not assigned'; end; My question is why after freeing the object is not nil and how will I make a to be nil after destructor without using a := nil ? Explanation: The variable a will only become nil when it is assigned nil .

how to achive - file write open on __del__?

 ̄綄美尐妖づ 提交于 2019-12-02 04:43:07
I m trying to do a some activity on class obj destruction. How do I achive file open in _del__ function? (I m using Python 3.4) class iam(object): def __init__(self): print("I m born") def __del__(self): f = open("memory_report.txt", "w") f.write("He gone safe") f.close() if __name__ == '__main__': i = iam() print("Script Ends. Now to GC clean memory") Output: I m born Script Ends. Now to GC clean memory Exception ignored in: <bound method iam.__del__ of <__main__.iam object at 0x00000000022F1A58>> Traceback (most recent call last): File "F:\Kumaresan\Code\Python\CommonLib\src\kmxPyQt