0
there must be a simpler way ?? destructors not going well😕
3 odpowiedzi
+ 2
Before you dismiss destructors, pls try to understand them deeply. Destructors are not only used in C++, they are used in many object oriented languages.
So how can I help you to understand destructors?
0
In java destructors are called automatically. This is called garbage collector. But in CPP you have to call destructor. Ex. Class MyClass, then destructor ~Myclass(){}. Destroying a class, you can save memory.
0
@Sabbir: What you said is either plain wrong or misleading. One doesn't have to call the destructor of a C++ object, normally. You can, however, but only on objects created by using "placement new" ("special" kind of new) you should.
The destructor is automatically called when one frees an object that has been allocated on the stack, global / static storage or with a "normal" new on the heap. Using placement new, the destructor does not get called, so one should do it manually. In other cases it's called for you.