Six special member functions of C + + class

Posted by whitsey on Mon, 24 Feb 2020 08:38:08 +0100

1. Set the parameters of six functions to run them first

//
// Created by liuhao on 20-1-1.
//


//=================================================================================
#include <iostream>

//using namespace std;

class Stu {

private:
    std::string name = "Anonymous person ";
    int age = 0;
    int *d = nullptr;
public:
    Stu() { std::cout << name << age << " Parameterless constructor executed!" << std::endl; };

    Stu(int a) { std::cout << name << age << " Parameter constructor executed!" << std::endl; };

    Stu(const Stu &s) { std::cout << name << age << " Copy constructor executed!" << std::endl; };

    Stu &operator=(const Stu &s) { std::cout << name << age << " Copy assignment operator function executed!" << std::endl; };

    Stu(Stu &&s) { std::cout << name << age << " Mobile constructor executed!" << std::endl; };

    Stu &operator=(Stu &&s) { std::cout << name << age << " Mobile assignment operator function executed!" << std::endl; };

    ~Stu() { std::cout << name << age << " Destructor executed!" << std::endl; };

};


int main() {

    Stu s1;

    Stu s2(100);

    Stu s3 = s2;

    Stu s4;
    s4 = s3;

    Stu s5 = std::move(s4);

    Stu s6;
    s6 = std::move(s5);

    return 0;
}
//=================================================================================


1. Output results

/home/liuhao/CLionProjects/Robot_modules/build/debug/bin/test/class_function
 Nameless 0 executes a parameterless constructor!
Anonymous 0 has implemented a parameter constructor!
Anonymous 0 executed copy constructor!
Nameless 0 executes a parameterless constructor!
Anonymous 0 executed copy assignment operator function!
Anonymous 0 executed the move constructor!
Nameless 0 executes a parameterless constructor!
Anonymous 0 executed the move assignment operator function!
Anonymous 0 executed the destructor!
Anonymous 0 executed the destructor!
Anonymous 0 executed the destructor!
Anonymous 0 executed the destructor!
Anonymous 0 executed the destructor!
Anonymous 0 executed the destructor!

Process finished with exit code 0

2. The realization of six functions

//
// Created by liuhao on 20-1-1.
//


////=================================================================================
#include <iostream>

//using namespace std;


class AA {

private:
    std::string name = "";
    int age = 0;
    int *d = nullptr;

public:
    explicit AA(std::string name) {      // It is better to add explicit to a single parameter constructor to avoid implicit conversion
        this->name = name;
        age = 123;
        d = new int(456);        // We need to give a direction here, because there's * AA UU d after that

        std::cout << this->name << age << " Parameterless constructor executed!" << std::endl;
        std::cout << this->name << age << " Address to which the pointer points:" << d << std::endl;
        std::cout << this->name << age << " The content of the address that the pointer points to:" << *d << std::endl;
        std::cout << this->name << age << " Address of the pointer itself:" << &d << std::endl;
        std::cout << "----------------------------------" << std::endl;
    };

    ~AA() {
        delete d;
        d = nullptr;

        std::cout << name << age << " Destructor executed!" << std::endl;
    };
};


class Stu {

public:
    std::string name = "Anonymous person ";
    int age = 0;
    int *d = nullptr;
    AA *p_internal = nullptr;   // Point to the internally generated internal object, release first and then leave empty in the destructor
    AA *p_external = nullptr;   // It refers to the external object passed externally, which is not managed in the destructor,

//=================================================================================
public:
    Stu() {
        name = "s1 ";
        age = 1;
        // TODO s1 is a null pointer. Although there is no new, the destructor can use delete
        std::cout << name << age << " Parameterless constructor executed!" << std::endl;
        std::cout << name << age << " Address to which the pointer points:" << d << std::endl;
        std::cout << name << age << " The content of the address that the pointer points to:" << "Null pointer does not point to, cannot dereference operation" << std::endl;
        std::cout << name << age << " Address of the pointer itself:" << &d << std::endl;
        std::cout << "----------------------------------" << std::endl;

    };

    Stu(int i, std::string name, AA *p_temp) {
        this->name = "s2 ";
        age = 2;

        d = new int(i);

        std::cout << this->name << age << " Parameter constructor executed!" << std::endl;
        std::cout << this->name << age << " Address to which the pointer points:" << d << std::endl;
        std::cout << this->name << age << " The content of the address that the pointer points to:" << *d << std::endl;
        std::cout << this->name << age << " Address of the pointer itself:" << &d << std::endl;
        std::cout << "----------------------------------" << std::endl;

        p_internal = new AA(name);

        if (p_temp == nullptr) {
            p_external = nullptr;
        } else {
            p_external = p_temp;
            p_temp = nullptr;               // Is this necessary?
        }
    };

//=================================================================================
    Stu(const Stu &s) {
        name = "s3 ";
        age = 3;

        if (s.d == nullptr) {
            d = nullptr;
        } else {
            //Open up new space
            d = new int();      // Do not use shallow copy (there are hidden dangers of dynamic members), try to use deep copy
            //Copy the original value.
            *d = *s.d;          // TODO note that s cannot be a null pointer, such as s1
        }

        std::cout << name << age << " Copy constructor executed(Deep copy)!" << std::endl;
        std::cout << name << age << " Address to which the pointer points:" << d << std::endl;
        std::cout << name << age << " The content of the address that the pointer points to:" << *d << std::endl;
        std::cout << name << age << " Address of the pointer itself:" << &d << std::endl;
        std::cout << "----------------------------------" << std::endl;
    };

//=================================================================================
    Stu(Stu &&s) noexcept {     // noexcept indicates that the function or operation will not be abnormal, which will give the compiler more optimization space.
        name = "s4 ";
        age = 4;                // Move assignment operator function, how to deal with s.name and s.age

        if (s.d == nullptr) {
            d = nullptr;
        } else {
            // The address pointed to by s is assigned to the current object this
            d = s.d;
            // s needs to be set to null, disconnect the pointer, and avoid two pointers pointing to the same space
            s.d = nullptr;
        }
        std::cout << name << age << " Mobile constructor executed!" << std::endl;
        std::cout << name << age << " Address to which the pointer points:" << d << std::endl;
        std::cout << name << age << " The content of the address that the pointer points to:" << *d << std::endl;
        std::cout << name << age << " Address of the pointer itself:" << &d << std::endl;
        std::cout << "----------------------------------" << std::endl;
    };

//=================================================================================
    Stu &operator=(const Stu &s) {
        name = "s5 ";
        age = 5;

        if (s.d == nullptr) {
            d = nullptr;
        } else {
            //Open up new space
            d = new int();      // Do not use shallow copy (there are hidden dangers of dynamic members), try to use deep copy
            //Copy the original value.
            *d = *s.d;          // TODO note that s cannot be a null pointer, such as s1
        }
        std::cout << name << age << " Copy assignment operator function executed!" << std::endl;
        std::cout << name << age << " Address to which the pointer points:" << d << std::endl;
        std::cout << name << age << " The content of the address that the pointer points to:" << *d << std::endl;
        std::cout << name << age << " Address of the pointer itself:" << &d << std::endl;
        std::cout << "----------------------------------" << std::endl;

        return *this;
    };

//=================================================================================
    Stu &operator=(Stu &&s) noexcept {     // noexcept indicates that the function or operation will not be abnormal, which will give the compiler more optimization space.
        name = "s6 ";
        age = 6;                // Move assignment operator function, how to deal with s.name and s.age

        if (s.d == nullptr) {
            d = nullptr;
        } else {
            // The address pointed to by s is assigned to the current object this
            d = s.d;
            // s needs to be set to null, disconnect the pointer, and avoid two pointers pointing to the same space
            s.d = nullptr;
        }
        std::cout << name << age << " Mobile assignment operator function executed!" << std::endl;
        std::cout << name << age << " Address to which the pointer points:" << d << std::endl;
        std::cout << name << age << " The content of the address that the pointer points to:" << *d << std::endl;
        std::cout << name << age << " Address of the pointer itself:" << &d << std::endl;
        std::cout << "----------------------------------" << std::endl;

        return *this;
    };

//=================================================================================
    ~Stu() {
        delete d;
        d = nullptr;

        std::cout << name << age << " Destructor executed!" << std::endl;

        delete p_internal;
        p_internal = nullptr;
    };
//=================================================================================




//=================================================================================
    Stu &operator&&(const Stu &s) {     // Redefined & & operator (DIY)
        name = "s9 ";
        age = 9;

        if (s.d == nullptr) {
            d = nullptr;
        } else {
            //Open up new space
            d = new int();      // Do not use shallow copy (there are hidden dangers of dynamic members), try to use deep copy
            //Copy the original value.
            *d = *s.d;          // TODO note that s cannot be a null pointer, such as s1
        }
        std::cout << name << age << " Copy assignment operator function executed!" << std::endl;
        std::cout << name << age << " Address to which the pointer points:" << d << std::endl;
        std::cout << name << age << " The content of the address that the pointer points to:" << *d << std::endl;
        std::cout << name << age << " Address of the pointer itself:" << &d << std::endl;
        std::cout << "----------------------------------" << std::endl;

        return *this;
    };
//=================================================================================

};





//=================================================================================

int main() {

// The six special member functions of the class are summarized as follows:
// Normal constructor (no parameter constructor: Stu(int i), parameter constructor: Stu(int i)) destructor: ~ Stu()
// Copy constructor: stu (const stu & S) copy assignment operator function: stu (stu & & S)
// Move constructor: stu & operator = (const stu & S) move assignment operator function: stu & operator = (stu & & S)

//=================================================================================
    AA aa("External objects ");
//=================================================================================
    Stu s1;                         // non-parameter constructor 
    Stu s2(100, "Internal objects ", &aa);   // Parametrical constructor
//=================================================================================
    Stu s3 = s2;                    // Copy constructor (deep copy) / / cannot use s1. There will be memory problems. See the above;
//=================================================================================
    Stu s4;
    s4 = s3;                        // Copy assignment operator function / / s1 cannot be used. There will be memory problems. See the above;
//=================================================================================
    Stu s5 = std::move(s4);              // Move the constructor / / s1 cannot be used. There will be memory problems. See the above;
    // TODO s3 can be reused
    std::cout << "**************************************** " << s4.d << std::endl;      // 0
    std::cout << "**************************************** " << s4.name << std::endl;   // s4
    std::cout << "**************************************** " << s4.age << std::endl;    // 4
//=================================================================================
    Stu s6;
    s6 = std::move(s5);                  // Move assignment operator function / / s1 cannot be used. There will be memory problems. See the above;
    // TODO s5 can be reused
    std::cout << "**************************************** " << s5.d << std::endl;      // 0
    std::cout << "**************************************** " << s5.name << std::endl;   // s5
    std::cout << "**************************************** " << s5.age << std::endl;    // 5
//=================================================================================
    Stu s9;
    s9 && s6;                       // Redefined & & operator (DIY)

    s9 && s6 && s3 && s2;           // Operator overloaded multiple times
//    S9 & & S6 & & S3 & & S2 & & S1; / / there is a null pointer in S1, which cannot participate in multiple overloads of operators, because there is dereference operation in the operator & & function
//=================================================================================
    return 0;
}


2. Output results

/home/liuhao/CLionProjects/Robot_modules/build/debug/bin/test/class_function
 The external object 123 executes a parameterless constructor!
Address pointed by pointer of external object 123: 0x10e7c20
 The contents of the address to which the pointer of the external object 123 points: 456
 Address of external object 123 pointer itself: 0x7ffdaabbeab8
----------------------------------
s1 1 executes the parameterless constructor!
Address pointed by s1 1 pointer: 0
 Content of address pointed by s1 1 pointer: null pointer does not point to and cannot dereference operation
 s1 1 address of pointer itself: 0x7ffdaabbeae8
----------------------------------
s2 2 implements the parameter constructor!
Address pointed by s2 2 pointer: 0x10e8050
 Content of address pointed by s2 2 pointer: 100
 Address of S2 pointer itself: 0x7ffdaabbeb28
----------------------------------
Internal object 123 executes a parameterless constructor!
Internal object 123 pointer to address: 0x10e80b0
 The contents of the address to which the internal object 123 pointer points: 456
 Address of internal object 123 pointer itself: 0x10e8098
----------------------------------
s3 3 executed copy constructor (deep copy)!
s3 3 address pointed by pointer: 0x10e80d0
 s3 3 content of address pointed to by pointer: 100
 S3 address of pointer itself: 0x7ffdaabbeb68
----------------------------------
s1 1 executes the parameterless constructor!
Address pointed by s1 1 pointer: 0
 Content of address pointed by s1 1 pointer: null pointer does not point to and cannot dereference operation
 s1 1 address of pointer itself: 0x7ffdaabbeba8
----------------------------------
s5 5 executed the copy assignment operator function!
S5 address pointed by pointer: 0x10e80f0
 S5 the content of the address pointed to by the pointer: 100
 S5 address of pointer itself: 0x7ffdaabbeba8
----------------------------------
s4 4 performs the move constructor!
s4 4 address pointed by pointer: 0x10e80f0
 s4 4 content of address pointed to by pointer: 100
 S4 the address of the pointer itself: 0x7ffdaabbe8
----------------------------------
**************************************** 0
**************************************** s5 
**************************************** 5
 s1 1 executes the parameterless constructor!
Address pointed by s1 1 pointer: 0
 Content of address pointed by s1 1 pointer: null pointer does not point to and cannot dereference operation
 s1 1 address of pointer itself: 0x7ffdaabbec28
----------------------------------
s6 6 executed the function of mobile assignment operator!
S6 address pointed by pointer: 0x10e80f0
 S6 content of address pointed by pointer: 100
 S6 address of pointer itself: 0x7ffdaabbec28
----------------------------------
**************************************** 0
**************************************** s4 
**************************************** 4
 s1 1 executes the parameterless constructor!
Address pointed by s1 1 pointer: 0
 Content of address pointed by s1 1 pointer: null pointer does not point to and cannot dereference operation
 s1 1 address of pointer itself: 0x7ffdaabbec68
----------------------------------
s9 9 executed the copy assignment operator function!
s9 9 address pointed to by pointer: 0x10e8110
 s9 9 content of address pointed to by pointer: 100
 s9 9 address of the pointer itself: 0x7ffdaabbec68
----------------------------------
s9 9 executed the copy assignment operator function!
Address pointed by s9 9 pointer: 0x10e8130
 s9 9 content of address pointed to by pointer: 100
 s9 9 address of the pointer itself: 0x7ffdaabbec68
----------------------------------
s9 9 executed the copy assignment operator function!
Address pointed by s9 9 pointer: 0x10e8150
 s9 9 content of address pointed to by pointer: 100
 s9 9 address of the pointer itself: 0x7ffdaabbec68
----------------------------------
s9 9 executed the copy assignment operator function!
Address pointed by s9 9 pointer: 0x10e8170
 s9 9 content of address pointed to by pointer: 100
 s9 9 address of the pointer itself: 0x7ffdaabbec68
----------------------------------
s9 9 executes the destructor!
s6 6 executes the destructor!
s4 4 executes the destructor!
s5 5 executes the destructor!
s3 3 performs the destructor!
s2 2 performs the destructor!
Inner object 123 performs a destructor!
s1 1 executes the destructor!
The external object 123 performs a destructor!

Process finished with exit code 0
Published 6 original articles, won praise 0, visited 124
Private letter follow

Topics: Mobile