+ 1
i am confused about the output of a destructor qusetion,what does destructor ACTUALL do when it is called....
here is the question: #include <iostream> using namespace std; class A{ public: ~A() {cout<<"A";} }; int main() { A a; a.~A(); return 0; } and the output of the program above is"AA" i wonder what does the destructor do,and is there anything happened in the memory when ~A() is called?
2 odpowiedzi
+ 1
Destructor does something important that must be done before the instance is deleted.
BUT THIS IS ALSO "FUNCTION".
This is just called automatically before the instance is deallocated.
So a.~A() will just print "A".
when a.~A() is called, nothing happens in the memory space, and there is NOTHING to happened even after a.~A() finishes.
(Of course, when a.~A() is called as program ends, after it finishes the memory of a will be deallocated.)
0
destructor is a function which run before program exit.. destructor is used to destroy object,,, free up the memory... u don't need to call it from main function.. it will automatically run before program exit..
#include <iostream>
using namespace std;
class A{
public:
A(){cout << "C" << endl} // print C when we create class A object..
~A() {cout<<"D";} // prints D before program exit
};
int main()
{
A a; // create class A object a
//costructor will called
// destructor will called
return 0;
}