下面这段文字是C++ primer中的一段文字:
为了理解他,我们来看这样一段代码:
Test.h 文件:
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Test
{
public:
Test(const string& s = "hello world") :str(new string(s)) { cout << "默认构造函数" << endl; };
Test(const Test& t);
Test& operator=(const Test& t);
Test(Test&& t)noexcept;
Test& operator=(Test&& t)noexcept;
~Test();
public:
string * str;
};
Test.cpp 文件
#include"Test.h"
Test::Test(const Test& t)
{
str = new string(*(t.str));
cout << "拷贝构造函数" << endl;
}
Test& Test::operator=(const Test& t)
{
cout << "拷贝赋值运算符" << endl;
return *this;