1. Destructors1. Destructors have no arguments and cannot be overloaded, so a class can have only one destructor. If the user does not have a definition, the compiler will generate it automatically.
2. static The local object is not destroyed at the end of the function call and therefore does not call the destructor .
The destructor for a static local object is called only at the end of the program, such as when the main function ends or calls the Exit Function .
3) If a global object is defined, only the destructor of the global object is called at the end of the program.
#include <iostream> #include <cstdlib>using namespace Std;class demo{private:double N; Double m; int i;}; void Func () {Demo *p = new Demo;} int main () {int i; for (I=1; i<=1000000; i++) {func (); } system ("Pause"); return 0;}
when the program runs to System ("pause"); When you open the Task Manager, you can see that this tiny program is taking up 32M of memory.
this is because each time the Func function is called, an object is created and P points to it. The function ends, releasing only the memory that the pointer variable p occupies, not the memory occupied by the object that P points to.
if the object memory is not reclaimed in the Func function, then you will never be able to reclaim it until the end of the program run (return) is recycled by the operating system, which is a typical memory leak. (New, delete appears in pairs)
4 new Delete call construction, destructor
malloc, free do not call construct, destructor
2. This
1. this, as an implicit parameter, is essentially a local variable of the member function, does not occupy the memory of the object, only assigns a value to this when a member function call occurs, and this is destroyed when the function call ends.
2. The member function is eventually compiled into an object-independent normal function, except for the member variables, all information is lost, so at compile time to add an additional parameter to the member function, the current object's first address passed in, so as to associate member functions and member variables. This extra parameter, in effect, is this, which is the bridge between the member function and the member variable Association.
3. Static member variables
-
static member variable is independent of the object, does not occupy the object's memory, but opens up memory outside of all objects, even if the object is not created. static member variable, like the normal static variable, allocates memory in the static data area at compile time and is released at the end of the program.
-
-
int student::num = 10; // private, protected, public initialization is the same.
4. Static member functions
1. when invoking a member function (non-static member function) of an object, the system assigns the starting address of the current object to the this pointer.
A static member function does not belong to an object, it has nothing to do with any object, so the static member function does not have the this pointer. Since it does not point to an object, you cannot access non-static members of that object.
It can be said that the fundamental difference between a static member function and a non-static member function is that a non-static member function has this pointer, whereas a static member function does not have the this pointer. This determines that a static member function cannot access non-static members in this class.
destructor this static member variable static member function