1. Assignment operator overloading
1. Timing: Assign a value to another existing object with an existing object . The assignment behavior that occurs after two objects have been created.
2. The assignment operator overload is provided by default and is no longer available once it is self-implemented.
3. The default is an equal-bit copy (that is, a shallow copy), which causes a re-destructor, which causes memory leaks.
4. The issue of the equal-bit copy in this situation:
1. Causing its own memory leak (the equal-bit copy is assigning its assigned amount of address to the assigned amount, so that the assigned amount of the original point of the address is not pointed, resulting in a memory leak.) )
2. Causes the refactoring (1 explained that two quantities point to the same address, causing its address to be deconstructed two times. )
3. Self-Assignment (belongs to the person, that is, deliberately write the self-assignment code, such as: a=a;)
5. Self-implementing assignment operator overloading (resolving the problem of equal bit copy)
1. The assigned value first releases the address that you point to
2. Create a space equal to the size of the assigned amount of memory, so that the assigned amount points to the new space
3. Finally assign the assigned value to the assigned amount.
4. Solve the self-assignment problem in 1: When entering the copy construction, it is determined whether it is itself, if the exit does not do any action.
5. The return statement in the self-implementing copy constructor is: Return *this, which is designed to achieve the even function (A=B=C)
Self-implementing assignment operator overload code:
MyString &mystring::operator = (const MyString &another) { if( this->_str=another._str) return *this; delete []_str; int len=strlen (ANOTHER._STR); _str=newchar[len+1]; strcpy (_STR,ANOTHER._STR); return *this;}
2.+ operator overloading (returns an object, not a reference)
Overloaded functions: CONCATENATE strings together
Realize:
1. First define a temporary intermediate class object to concatenate two strings together.
2. Release the data member memory in the object.
3. Reassign the new space (length of the total length of two strings), if the following with the strcpy function should be emptied first (the newly allocated space is likely to hold the garbage value, and strcpy is the copy from the start, if there are some garbage values, the result will be wrong. ), with memset (to clear the memory, 0, the length of memory) function empty
4. Use the STRCAT function to concatenate two strings together (first connected, second in connection, order not reversed).
5. Returning Intermediate temporary objects
String string::operator + (const String &anoter) {string temp; delete []temp._str; int Len=strlen (this ->_STR); Len +=strlen (ANOTER._STR); Temp._str =new char [Len+1 ]; memset (Temp._str, 0 , Len+1 ); strcat (Temp._str, this ->_str); strcat (TEMP._STR,ANOTER._STR); return temp;}
Added the result of the Memset function:
The result of the memset function is not added:
3. Comparison operator overloading (subscript operator [], >, <, = =)
char& String::operator [] (IntIDX) {ReturnThis->_STR[IDX];}BOOL String::Operator > (Const String &Another) {IfThis->_str>ANOTHER._STR)ReturnTrue;ElseReturnFalse;}BOOL String::Operator < (Const String &Another) {Ifthis->_str<another._str) return trueelse return false; bool string::operator = = ( const String &another) { if (this->_str== ANOTHER._STR) return true else return false;}
4.const modifier to decorate data members, member functions, class objects
1. When modifying a data member,
The initialization location can only be in the parameter list.
Const-Decorated data member, cannot be modified
2. When modifying member functions,
After the position is placed in the function declaration, the implementation body is preceded by "Void print () const { }", which requires the Const keyword at both the declaration and the definition.
Meaning: The class data members are not modified by any means, i.e., const and non-const data members can be accessed, but non-const data members cannot be modified, and cannot be modified by their class member functions, only const member functions can be accessed.
Can form an overload
A const object can only call a const member function.
A non-Const member object that calls the non-const member function first, or the const member function if none is available.
3. Modifying class objects
The const modifier, which is from the level of the function, cannot modify the data
The const modifier object, which is from the object's plane, cannot modify the data and can only call the const member function
5.static modifier (static: Static variable, static function)
Can modify global variables, local variables in C
Modifying global variables: Changing scopes so that they are limited to this file
Modify a local variable: Change the life cycle (local variables are followed by the Sui, used to eliminate, with the static modification of the same as the main function life cycle), storage location (unmodified local variables placed on the stack, the static decoration of the data segment of the BSS segment (uninitialized) or RW segment (initialization))
In C + +
Within the class, it is used to share data among the race objects.
Normal data members have room when generating objects. Static data members have opened up space when the class is declared.
Static data members belong to both classes (which can be accessed with classes) and also to objects (which can also be accessed with objects), but ultimately belong to classes. "That is, when there is no object build, the static data member can be accessed by the class name"
Static decorated data member: defined within a class, defined outside the class: type name Class Name:: Variable name = initial value;
Static modifier member function: Used only to manage static data members.
The member functions of the static modifier belong to both the class (which can be accessed with the class) and also to the object (which can also be accessed with the object), but eventually belong to the class. "That is, when there is no object build, you can access the static member function by using the class name"
The static decorated member function, because it belongs to a class, so there is no this pointer. You cannot access non-static data members and member functions.
6. Pointers to class members
Pointers to class data members:
Definition: Type name Class name::* pointer
Initialize: Type name Class name::* pointer =& class name:: Non-static data member
Pointers to non-static data members must be defined in relation to the class, and must be associated with a specific object when used
Dereference: Class object name. * Pointer or class object name->* pointer
Pointers to class member functions:
Definition: Type name Class name::* pointer (parameter list)
Initialize: Type name Class name::* pointer (argument list) =& class name:: Non-static member function
Dereference: Class object name. * Pointer or class object name->* pointer (parameter list)
Wang C + + video (best C + + video, not one of them) Learning Note 4