I went to HP for an interview two days ago and asked if the constructor and destructor can be displayed and called.
When I got back to the Internet and checked it, both of them can be displayed and called. As follows:
Class
{
Public:
A ()
{
Cout <"A cnostructor" <Endl;
}
~ A ()
{
Cout <"A destructor" <Endl;
}
};
Void main ()
{
A;
A. A: A (); // display the call constructor. If it is written as a. A (), an error is returned.
A .~ A (); // display that the Destructor is called, but object A is not destroyed at this time.
}
The output result is as follows:
A constructor
A constructor // displays the result of calling the constructor.
A destrucotr // displays the result of calling the destructor. At this time, the object is not destroyed.
A destructor // The Destructor is automatically called when the object is destroyed.
Conclusion: It is shown that calling constructor and destructor is the same as calling a common function, and does not mean creating or destroying objects. However, if the constructor dynamically allocates space, it is displayed that memory leakage occurs when calling the constructor. If the constructor releases the dynamically allocated space, the unified memory is released multiple times, and a serious error occurs.