+ 1
Why when I created an object, it gets destroyed immediately.
I made a constructor and destructer function. https://code.sololearn.com/cmJmudJ7P1M2/?ref=app
2 ответов
+ 3
What did you expect as output?
The object here doesn't get destroyed immediately : it simply get destroyed at the end of then scope in which it was defined. It is called the lifetime of an object.
+ 1
A destructor is called when a class is destroyed. It is because as you create an instance of your object , the progam reaches the end and destroys it. Try putting other code , You will see the destructor called only after everything is complete.
There are other reasons as well for the destructor to be called. Such as:
1.If a 'raw pointer' pointing to the object is deleted.
2. If the object goes out of scope.
Have a good day,
Ćheyat