Why copy constructor parameters need to add const and reference

#include <iostream>
using namespace std;
class CExample
{
public:
    CExample(int x) :m_nTest(x) //Constructor with arguments
    {
        cout<< "constructor with argument."<<endl;
    }
    CExample(const CExample & ex) //copy constructor
    {
        m_nTest = ex.m_nTest;
        cout << "copy constructor."<<endl;
    }
    CExample& operator = (const CExample &ex)//Assignment function (assignment operator overloading)
    {
        cout << "assignment operator." << endl;
        m_nTest = ex.m_nTest;
        return *this;
    }
    void myTestFunc(CExample ex)
    {
    }
private:
    int m_nTest;
};

int main()
{
    CExample aaa(2);
    CExample bbb(3);
    bbb = aaa;
    CExample ccc = aaa;
    bbb.myTestFunc(aaa);
    system("pause");
    return 0;
}

1.1 [Output result]

1.2 [Analysis Results]

The first output: constructor with argument. //CExample aaa(2);

The variable aaa is created here, and it also has parameter 2 when it is created, then call the constructor with parameters

The second output: constructor with argument. //CExample bbb(3);

Analysis is the same as the first

The third output: assignment operator. //bbb = aaa;

bbb has been created before, so this will not call the constructor, but just assign aaa to bbb, so call the
fourth output of the assignment function: copy constructor. //CExample ccc = aaa;

The difference between this and the previous one is that the bbb has been created before, and the ccc here is created after this statement, so here is the assignment of aaa to ccc while creating ccc, so this call must be Constructor, and because aaa needs to be assigned to ccc, the copy constructor is called.

The fifth output: copy constructor. // bbb.myTestFunc(aaa);
here is a call to a self-written myTestFunc function, in which the parameters in this function are not referenced, that is the way of passing by value. That is, the compiler first creates an object whose type is CExample and whose name is ex, and then passes the value of aaa to ex (a characteristic of value passing method), which is equivalent to executing a CExample ex = aaa statement. Analysis of the fourth output shows that this requires calling the copy constructor. So output copy constrctor.

 

2. Answer questions

2.1 Why use citations?

  [Wrong answer] Personal first reaction: In order to reduce a memory copy.

  [Correct answer] From the fifth output analysis in the previous section, we can see that the copy constructor is actually called when bbb.myTestFunc(aaa); is executed. If the parameter of our copy constructor is not a reference, then when bbb.myTestFunc(aaa);, CExample ex = aaa; is called, and because ex has not been created before, the copy constructor needs to be called again, so CExample ex is executed again = aaa;, so the recursive call goes on forever.

  Therefore, the copy constructor must take parameters of reference type, and this is also mandatory for the compiler.

 

2.2 Why use const?

  [Correct answer] If the value of the reference type parameter will not be changed in the function, the effect of adding or not adding const is the same. And without const, the compiler will not report an error. However, for the safety of the entire program, const is added to prevent accidental modification of the reference type parameter value.

 

——If there is something wrong, you are very welcome to give guidance!

——[Thanks] The information comes from http://blog.csdn.net/tunsanty/article/details/4264738

——[Thanks] The information comes from http://blog.csdn.net/sinat_36053757/article/details/70597567

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325773306&siteId=291194637