Assignment operator copy function
Example:
#include <iostream> using namespace std; class Test{ public: Test():n(0),m(0){} Test(int n,int m):n(n),m(m){} Test& operator=(const Test& t){ //Overloaded assignment operator, overriding the default assignment between objects of the compiler cout << this << " operater=" <<endl; n = t.n; //Copy member content m = t.m; return *this; //There must be a return value, otherwise an error will be reported } void Print(){ cout << this << ":" << n << "." << m << endl; } private: int n; int m; }; int main(){ Test t(100,150); t.Print(); Test s; s.Print(); s=t; //The equal sign here has become a function, which is equivalent to S. operator (T); s.Print(); }
In general, the compiler will have the function of assigning values between objects, and there is no need to overload the assignment operator. This example is for demonstration. Unless the object member contains pointer type, deep copy is required to prevent memory leakage due to secondary memory release.
Example two:
#include <iostream> #include <cstring> using namespace std; class String{ private: char *str; public: String():str(NULL){}; //Define default constructor String(const char* s){ //Defining a constructor with parameters str = new char[strlen(s)+1]; strcpy(str,s); } String(const String& s){ //Define copy constructor if(NULL==s.str) {str = NULL;} else { str = new char[strlen(s.str)]; //Deep copy strcpy(str, s.str); } } String& operator=(const String& s){ //Define assignment symbol overload function if(&s == this) //Avoid self assignment, otherwise the following if statement will delete the original information stored by itself return *this; if(str != NULL) //Delete the originally requested space delete [] str; if(NULL == s.str) //Set to empty if the original object is empty str = NULL; else{ str = new char[strlen(s.str)+1]; //Deep copy strcpy(str,s.str); } return *this; //Return object } void Print(){ cout<<this<<endl; } }; int main(){ String str1; //Call default constructor str1.Print(); String str2("Hello"); //Call constructor with parameters str2.Print(); String str3(str2); //Call copy constructor str3.Print(); str1 = str2; //Call assignment symbol overload function str1.Print(); str2 = str2; //Test self assignment str2.Print(); }
Note the difference:
Class name object 2 = object 1; / / the call to the copy constructor will be triggered Object 2 = object 1; / / call of assignment operator overload function will be triggered
Deep copy and light copy
friend function
See the following example:
After the Test class is defined as a friend class of the Simple class, all member functions in the Test class can access the private members of the Simple class.
After defining friend functions and friend classes, because they can access private member variables in the class, the security of the class is broken, so in general, friends are not easy to use.
const qualifier
Example:
#include <iostream> using namespace std; class Simple{ int n; public: Simple(int n):n(n){} void Print()const{ cout << n << endl; //Modifying member functions of member variables is not allowed } //The const qualifier is placed in front of the function, which means that the return value of the function cannot be changed. Generally, it has no specific function void Increase(){ ++n; //Member functions that allow modification of member variables } }; //There are two types of member functions: //1. Non const member functions can modify member variables and can only be called by const members //2.const member function, not allowed to modify member variables, can be called by const object and non const object int main(){ int const n = 10; // n + + / / after the general const variable definition is initialized, it cannot be modified string const s("sbc"); const Simple t(100); t.Print(); // t. If Increase() / / T is a const object, you cannot call Increase() of a member function that allows modifying member variables Simple k(10); k.Print(); k.Increase(); //k is not a const object, so it can call either Increase() or Print() k.Print(); }
Object as a function parameter:
If the parameters of a function are not const qualified, you cannot pass in a const object, because the object may be modified inside the function to report an error.
If the parameter of a function is const limited, it can be passed in either const object or non const object (even if the non const object is passed in, the function will not modify it and cause no loss). The most important thing is that such a function can accept anonymous objects. (the copy constructor simple (const test & T), whose parameters are const limited, because the copy object it receives may be const object, non const object or anonymous object. The reference type is used for the object itself to avoid triggering the constructor.)
If the return value of a function is const qualified, it cannot be connected with a variable other than const