Configuration function and destructive function in C ++

zhaozj2021-02-16  83

In the previous "New NEW in C ", I made a very idiotless error: New is an operation used to dynamically allocate memory, and the member function is static allocation memory. NEW an object, just assigns dynamic space to his member variable.

In order to understand the constructor and the destructor, the following program is written:

Class myclass {public: myclass (); ~ myclass (); void myfun (); private: int Meclass () {cout << "Myclass ()" <<} myclass () "<< Endl;} myclass :: ~ Myclass () {cout << "~ myclass ()" << Endl;} void myclass :: myfun () {member = 8; cout << "myfun out: << means << endl;

INT _TMAIN (int Argc, _tchar * argv []) {void test1 (); void test2 (); void test3 ();

COUT << "test1 ():" << endl; test1 (); cout << "test2 ();" << endl; test2 (); cout << "test3 ():" << endl; test3 () GetChar (); return 0; return 0;}

Void test1 () {myclass_myclass; _myclass.myfun ();

Void test2 () {myclass * _myclass = new myclass (); _myclass-> myfun (); void test3 () {myclass * _myclass = new myclass (); _myclass-> myfun (); delete _myclass;} // --------------- Out ----------------------------------- /

Test1 (): myclass () Myclass () TEST2 (); myclass () myfun out: 8test3 (): myclass () Myfun Out: 8 ~ myclass ()

in conclusion:

If a non-pointer-type object variable (this is accurate?), The compiler will automatically call constructor and destructive functions in the cycle of variables. If new objects, the pointer variable is defined, you must use Delete So, can you say: New and DELETE must be paired? Probably!

转载请注明原文地址:https://www.9cbs.com/read-13583.html

New Post(0)