Tag: Ack address OLA stack Body example line main []
One: The three main functions involved in the implementation of the String class: copy constructor, copy assignment function, destructor
The following is the class body part:
classstring{ Public: String (Const Char* cstr=0); String (Conststring&str); String&operator=(Conststring&str); ~String (); Char* GET_C_STR ()Const{returnm_data;}Private: Char*m_data;};
Next is the concrete definition of the constructor, copy constructor, copy assignment function, and destructor in the string class, and note that the above function is written as an inline function:
(1) The definition of a constructor in the string class:
inlinestring::string (constChar* CStr) { if (CStr) { New Char [Strlen (CStr) +1]; strcpy (M_data, CStr); } Else { newchar[1]; ' / ' ; }}
(2) Definition of copy constructor:
inlinestring::string (const string& str) { newchar1 ]; strcpy (M_data, str.m_data);}
(3) Definition of copy assignment function:
inlinestring& String::operator= (const string& str) { if ( This = = &str )return * this; Delete [] m_data; New Char 1 ]; strcpy (M_data, str.m_data); return *this;}
(4) destructor:
inlinestring::~String () { delete[] m_data;}
Two: Stack stacks, heap heaps
A Stack is a piece of memory space that exists in a scope. For example, when a function is called, the function itself forms a stack to hold the parameters it receives and the return address. Any variable declared in the body of a function that uses a block of memory is taken from the stack.
Heap, a piece of global memory space provided by the operating system, which can be dynamically allocated to obtain a chunk.
Example:
class Complex {...} ... { Complex C1 (1,2); ComplexNew Complex (3);}
Where the space occupied by C1 comes from the stack, and Complex (3) is a temporary object, the space occupied by the new self-heap is allocated dynamically and is directed by P.
Three, the life cycle
(1) Life cycle of stack object
class Complex {...}; ... { Complex C1 (1,2);}
C1 is a stack object whose life ends at the end of the scope. object in this scope, also known as Auto object, because it is automatically cleaned up.
(2) The life cycle of the static local objects
class Complex {...}; ... { static Complex C2 (1,2);}
C2 is a static object whose life persists after the end of the scope until the end of the entire program.
(3) The life cycle of global objects
class Complex {...}; ... Complex c3 (1,2); int Main () { ...}
C3 is a global object whose declaration ends after the end of the entire program and can be treated as a static object whose scope is the entire program.
(4) The life cycle of the heap objects
class Complex {...}; ... { Complex* p=new Complex; ... Delete p;}
Iv. New and delete
NEW: Allocate memory first, then call constructor
Delete: The destructor is called first, and then the memory is freed
V. About static
There are two ways to call the static function:
(1) Call through object;
(2) Call through class name
Second week notes C + + Boolan