C ++ Destructor Code

I have this simple C ++ code, but I don't know how to use the destructor.

class date { public: int day; date(int m) { day =m; } ~date(){ cout << "I wish you have entered the year \n" << day; } }; int main() { date ob2(12); ob2.~date(); cout << ob2.day; return 0; } 

the question arises: what should I write in the code of the destructor, that after calling the destructor, it will delete the variable "day". ???

+6
source share
7 answers

You should not explicitly call your destructor.

When you create your object on the stack (like you), all you need is:

 int main() { date ob2(12); // ob2.day holds 12 return 0; // ob2 destructor will get called here, after which it memory is freed } 

When you create your object on the heap, you need to delete your class before calling its destructor and freeing memory:

 int main() { date* ob2 = new date(12); // ob2->day holds 12 delete ob2; // ob2 destructor will get called here, after which it memory is freed return 0; // ob2 is invalid at this point. } 

(Failure to comply with deletion in this last example will result in memory loss.)

Both methods have their advantages and disadvantages. The stack path is VERY fast with the allocation of memory that the object will occupy, and you do not need to explicitly delete it, but the stack has limited space, and you cannot easily and quickly move these objects.

A bunch is the preferred way to do this, but when it comes to performance, it stands out slowly and you have to deal with pointers. But you have much more flexibility about what you are doing with your object, and working with pointers faster, and you have more control over the lifetime of the object.

+1
source

You rarely have to call the destructor explicitly. Instead, the destructor is called when the object is destroyed.

For an object of type ob2 , which is a local variable, it is destroyed when it goes out of scope:

 int main() { date ob2(12); } // ob2.~date() is called here, automatically! 

If you dynamically allocate an object using new , its destructor is called when the object is destroyed using delete . If you have a static object, its destructor is called when the program terminates (if the program terminates normally).

If you do not create something dynamically with new , you do not need to do anything explicit to clear it (for example, when ob2 is destroyed, all its member variables, including day , are destroyed). If you create something dynamically, you must make sure that it is destroyed when you finish with it; It is best to use the so-called smart pointer to ensure that this cleaning is automatically processed.

+14
source

You do not need to explicitly call the destructor. This is done automatically at the end of the scope of the ob2 object, that is, at the end of the main function.

In addition, since the object has automatic storage, its storage does not need to be deleted. This is also done automatically at the end of the function.

Manually calling destructors is almost never needed (only in low-level library code), and manually deleting memory is only required (and only a valid operation) when the memory was previously obtained using new (when you work with pointers).

Since manual memory management is prone to leaks, modern C ++ code tries not to explicitly use new and delete . When it is really necessary to use new , the so-called "smart pointer" is used instead of the usual pointer.

+9
source

Only in special circumstances do you need to call the destructor directly. By default, the destructor will be called by the system when an automatic storage variable is created, and it falls out of scope or when an object dynamically allocated with new is destroyed with delete .

 struct test { test( int value ) : value( value ) {} ~test() { std::cout << "~test: " << value << std::endl; } int value; }; int main() { test t(1); test *d = new t(2); delete d; // prints: ~test: 2 } // prints: ~test: 1 (t falls out of scope) 

For completeness (this should not be used at all), the syntax for calling the destructor is similar to the method. After the destructor starts, the memory is no longer an object of this type (it should be processed as raw memory):

 int main() { test t( 1 ); t.~test(); // prints: ~test: 1 // after this instruction 't' is no longer a 'test' object new (&t) test(2); // recreate a new test object in place } // test falls out of scope, prints: ~test: 2 

Note: after calling the destructor on t , this memory location is no longer test , that is, the reason for reconstructing the object using the new placement.

+2
source

In this case, your destructor does not need to delete the day variable.

You only need to call the delete in memory that you allocated with the new one.

This is what your code would look like if you used new and delete to start the call to the destructor

 class date { public: int* day; date(int m) { day = new int; *day = m; } ~date(){ delete day; cout << "now the destructor get called explicitly"; } }; int main() { date *ob2 = new date(12); delete ob2; return 0; } 
0
source

Although the destructor seems to be something you need to call to get rid of or "destroy" your object when you finish using it, you should not use it that way.

A destructor is something that is automatically called when your object goes out of scope, that is, when the computer leaves the “braces” in which you instantiated your object. In this case, when you leave main (). You do not want to call it yourself.

0
source

Here you may be confused by undefined behavior. There are no rules in the C ++ standard as to what happens if you use an object after running its destructor, as this behavior is undefined, and therefore the implementation can do whatever it likes. As a rule, compiler developers do nothing special for undefined behavior, and therefore there is an artifact of what was done by other design decisions. (Sometimes this can cause very strange results.)

Therefore, after starting the destructor, the compiler has no additional obligations regarding this object. If you do not reference it again, it does not matter. If you refer to it, this behavior is undefined, but from a standard point of view, the behavior does not matter, and since the Standard does not say anything, most compiler developers will not worry about what the program does.

In this case, the easiest way is to leave the object untouched, since it does not hold resources, and its storage was allocated as part of the function launch and will not be returned until the function exits. Therefore, the value of the data item will remain unchanged. Naturally, what the compiler should do when it reads ob2.day is access to the memory cell.

Like any other example of undefined behavior, the results may change with any change in circumstances, but in this case, they probably will not. It would be nice if the compilers would catch more cases of undefined behavior and diagnose problems, but compilers cannot detect all undefined behavior (some of them occur at runtime), and often they don’t check the behavior I don’t think is possible.

0
source

All Articles