相关内容:
- 需要重载赋值运算符重载函数:
- 用非类A类型的值为类A的对象赋值时(当然,这种情况下也可以不提供相应的赋值运算符重载函数,而只提供相应的构造函数)。
- 当用类A类型的值为类A的对象赋值,且类A的数据成员中含有指针的情况下,必须显式提供赋值运算符重载函数。
代码相关内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using namespace std;
class String
{
public:
String(char const *chars = "");
String &operator=(String const &); // (1)
String &operator=(char const *); // (2)
String &operator=(char); // (3)
void print();
private:
char *ptrChars; // 私有成员是一个字符指针;
};
void String::print()
{
cout << ptrChars << endl;
}
String &String::operator=(String const &str)
{
// 首先判断所占内存大小是否一样,如果不同需要重新分配内存;
if (strlen(ptrChars) != strlen(str.ptrChars))
{
char *ptrHold = new char[strlen(str.ptrChars) + 1];
delete[] ptrChars; // 每一个new的成员都需要进行delete;
ptrChars = ptrHold;
}
std::strcpy(ptrChars, str.ptrChars);
return *this; // 赋值操作必须返回对*this指针的引用;
}
String::String(char const *chars)
{
// 如果是Null指针,则将chars变为空字符串;
chars = chars ? chars : "";
// +1 是因为用字符数组来保存,字符数组的最后一位是\0,因此加1;
ptrChars = new char[std::strlen(chars) + 1];
std::strcpy(ptrChars, chars);
}
int main()
{
String s("hello");
String s2("HTL2018");
s.print();
s2.print();
s = s2; // 赋值操作;
s.print();
// s = "dfa"; // 调用赋值操作符重载方式(2),但是要定义相关函数,本程序中没有再定义,只是简单说明一下;
// s = 'A'; // 调用赋值操作符重载方式(3),但是要定义相关函数,本程序中没有再定义,只是简单说明一下;
return 0;
}代码执行结果:
参考链接:
_CRT_SECURE_NO_WARNINGS错误提示,解决办法
C++中的赋值运算符重载函数(operator=)