Copy constructors and Assignment Operators

后端 未结 3 1347
甜味超标
甜味超标 2020-12-06 14:37

I wrote the following program to test when the copy constructor is called and when the assignment operator is called:


#include 

class Test
{
public:
    T         


        
3条回答
  •  再見小時候
    2020-12-06 15:17

    Your first set is according to the C++ standard, and not due to some optimization.

    Section 12.8 ([class.copy]) of the C++ standard gives a similar example:

    class X {
        // ...
    public:
        X(int);
        X(const X&, int = 1);
    };
    
    X a(1);     // calls X(int);
    X b(a, 0);  // calls X(const X&, int);
    X c = b;    // calls X(const X&, int);
    

    The last line would be the one matching your case.

提交回复
热议问题