Constructor: called when creating an object of a class member function constructor can override the default constructor: no-arg constructor name is the same as the name of the class There are arguments constructor: name and the name of the class parameter is not the class members of the same parameter list But to assign the value of the class members copy (copy) constructor: a special constructor The copy constructor’s argument list must have a reference variable destructor of the type: the class member function that is called when an object is deleted has no arguments. The destructor can’t be overloaded C++ overloading :C++ allows multiple functions and operators of the same name with similar functions in the same scope. Operator overloading: an overloaded operator is a function with a special name consisting of the keyword operator and the operator symbol to be overloaded after it

Copy constructor

Copy constructors are constructors that take one object as the initial value of another class object. The copy constructor is called in three cases

class Chess{};
//------------- the first way ---------------//
// Use one object to explicitly initialize another object
Chess chess_one;
Chess chess_two = chess_one;

//------------- the second way ---------------//
// Pass an object to the function as a value
void getName(Chess a){}
getName(chess_one);

//------------- the third way ---------------//
// Return an object from a function as the return value of the function
Chess setName(a){
    Chess chess_one;
    / /...
    return chess_one;
}
Copy the code

Example 1:

The program file constructor is stored in the file directory

#include <iostream>
#include <string>
using namespace std;
class MyStr{
private:
    string name;
public:
    MyStr(a);// No argument constructor
    MyStr(string n);// There are argument constructors
    MyStr(const MyStr &s);// The copy constructor works because there are no Pointers in the data member. We will discuss shallow and deep copies later
    ~MyStr(a);// destructor
    MyStr &operator= (const MyStr &s){// Override the = operator because we're going to use the = object for assignment next remember the ampersand
    // const functions can accept both const and nonconst arguments. Otherwise, functions can only accept nonconst arguments
        cout<<"Overloaded assignment operator ="<<endl;
        this->name=s.name;
        return *this;// The return value is a reference to the assigned person, i.e. *this, which avoids a copy when the function returns and improves efficiency
        // A =b=c
    }
    friend ostream &operator<<(ostream &os,const MyStr &s){
        os<<s.name<<endl;// The function declares friend because name is a private member}}; MyStr::MyStr(){
    name="syh";
    cout<<"Default no-argument constructor"<<endl;
}
MyStr::MyStr(string n){
    name=n;
    cout<<"Parameterized constructor"<<endl;
}
MyStr::~MyStr() {
    cout<<"Call destructor"<<endl;
}
MyStr::MyStr(const MyStr &s) {
    name=s.name;
    cout<<"Copy constructor"<<endl;
}
MyStr::MyStr(const char *c) {// With this constructor we can initialize MyStr STR ="sdas"
    cout<<"Copy constructor (pointer string)"<<endl;
    name=c;
}
int main(a) {
    MyStr str1;// Call the no-argument constructor
    MyStr str2("outlook");// Call the argument constructor
    MyStr str3=str2;// Call the copy constructor
    MyStr str4;// Call the no-argument constructor
    str4=str2;// Call the overloaded operator =
    cout<<str1<<str2<<str3<<str4;//<< is designed to print the contents of the object
    return 0;// Call the destructor four times
}
Copy the code

Output result:

Default no-argument constructor has parameter constructor Copy constructor Default no-argument constructor overload assignment operator = syh Outlook Outlook Outlook calls destructor calls destructor calls destructor calls destructor calls destructor calls destructor calls destructorCopy the code

Program modification &

Line 12 in the program example

MyStr &operator=(const MyStr &s);
Copy the code

If you remove the ampersand = overload it’s going to change a little bit if you don’t add ampersand it’s going to make a copy of the argument when the function is called and the result of the program is going to be

Default no-argument constructor has argument constructor Copy constructor Default no-argument constructor overload assignment operator = copy constructor# newCall the destructor# newSyh Outlook Outlook Outlook calls the destructor. Calls the destructor. Calls the destructorCopy the code

So when you do str4=str2 assignment and pass the parameter after you call the operator = overload it generates a temporary object and that temporary object we’ll call pro pro gets the value of STR2 and then it calls the copy constructor and passes that value to STR4 and pro is destructed MyStr str4; str4=str2; Code equivalent to

MyStr str4; pro=str2; // Temporary object operator = overload assignment# Temporary object ProMyStr str4=pro; // Copy constructCopy the code

Shallow copy and deep copy

Shallow copy

Shallow copy MyStr str3=str2; Just copy the values of the data members in str2 to the data members in str3 and do nothing else so that STR2 and STR3 have the same name value and they point to the same area of memory and when we change the name value of STR2 the name of STR3 will be changed When executing the destructors for STR2 and STR3, the same memory area is freed twice and the program crashes

Deep copy

MyStr str3=str2; MyStr str3=str2; New address space is applied for str3 name and str2 name is copied into STR3 name so that str2.name and str3.name are independent

Assignment operator overload

C++ specifies that an assignment operator overload function must be a non-static member function of a class. It cannot be a static member function or a friend function

Can’t be a static member functions: the static member function only static data members of the action class non-static member cannot be used Can’t be a friend function: when the procedures prescribed by the c + + is not explicitly provide a reference to this class or the class as a parameter of the assignment operator overloading function The compiler automatically provides a But the function does not belong to this class When we provide an overload of the assignment operator for a friend, the system will automatically provide one such that the program will be ambiguous when it executes. C++ forces the overloaded assignment operator function to be defined only as a member function of the class

Overloaded functions cannot be inherited either: if the subclass does not provide the assignment operator itself, overloaded functions simply use the superclass’s overloaded functions. The superclass’s overloaded functions can only call its own data members, and the data members of the subclass cannot be used

Assignment operator functions should avoid self-assignment. If self-assignment occurs, return *this; : Assigning values to yourself is pointless and calling other functions is expensive

If a class is contained in the data members of a pointer p hypothesis with assigned to a pointer q p need to p, pointing to the original space delete off (because the space of the pointer p are often new to before Without space for p redistribution when the original space delete off can cause a memory leak) if it is from the assignment So q and p is the same pointer Before assigning to the same memory space, we need to delete the original space of P, and then the object pointed to by q will also be deleted and cannot be assigned

There are a few things to consider when defining an assignment operator function: Return a reference to the type of the returned value and return a reference to the object itself before the end of the function (*this) continuous assignment is allowed only if a reference is returned (a=b=c). Otherwise, if the return value of the function is void, continuous assignment will not be allowed and the program will not pass compilation. If the parameter passed is not a reference but an object, call the copy constructor once from the parameter to the argument. Declaring the parameter as a reference can avoid unnecessary waste and improve the efficiency of the code. At the same time, we don’t change the window sill of the passed object in the assignment operator function So add the const keyword 3. If you forget to free the existing memory of an object before allocating new memory, a memory leak occurs. If *this is the same object as the current object (*this), the object will be freed. If *this is the same object, the object will be freed The memory that passed in the parameter is also freed and there is no longer anything to assign to

MyStr &operator= (const MyStr &s){// Override the = operator because we're going to use the = object for assignment next remember the ampersand
    // const functions can accept both const and nonconst arguments. Otherwise, functions can only accept nonconst arguments
        cout<<"Overloaded assignment operator ="<<endl;
        if(this! =&s){MyStr strT(s);// Implicitly call the constructor

            char *pTemp=strT.name;
            strT.name=name;
            name=pTemp;
        }
        return *this;// The return value is a reference to the assigned person, i.e. *this, which avoids a copy when the function returns and improves efficiency
        // A =b=c
    }
Copy the code

This function creates a temporary object, strT, and swaps strt. name with the name of the object itself. Since strT is a local variable that leaves the if scope, the destructor of strT is automatically called This is equivalent to calling the destructor itself to free the object’s memory

Original address Ruojhen’s personal blog