c++中拷贝构造函数与赋值运算符重载函数的关系

拷贝是已有对象给未知对象,这个调用的是

而赋值是两个已有对象

class Test{

public:
    Test& operator=(const Test&){ std::cout << "执行了赋值操作" << std::endl; return *this; };
    Test(const Test&){ std::cout << "执行了拷贝操作" << std::endl; }
};

int main(){
    Test t1;

    // 拷贝
    Test t2(t1);
    Test t3 = t1;

    // 赋值
    Test t4, t5;
    t5 = t4 = t1;

    return 0;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章