运算符重载

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了运算符重载相关的知识,希望对你有一定的参考价值。

 

对于二元运算符重载,如果运算符左边是类,那么就在该类内部成员函数重载运算符即可。

如果运算符左边不是类,而是常量,那么就必须在类的外部定义一个运算符重载函数。

 

1 重载等号=

2 重载加号+

 

1 重载等号=

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <iostream>
 3 using namespace std;
 4 
 5 class mystring
 6 {
 7 public:
 8     char *s;
 9     mystring()
10     {
11         s = new char[1024];
12         cout << "mystring" << endl;
13     }
14     mystring(const mystring &it)//实现深拷贝
15     {
16         cout << "copy string" << endl;
17         s = new char[1024];
18         memset(s, 0, 1024);
19         strcpy(s, it.s);
20         cout << "mystring" << endl;
21     }
22     mystring operator=(const mystring &it)//运算符重载,实现深拷贝,第1次
23     {
24         cout << "=operator" << endl;
25         memset(s, 0, 1024);
26         strcpy(s, it.s);
27         return *this;
28     }
29     mystring operator=(const char *str)//运算符重载,第2次
30     {
31         memset(s, 0, 1024);
32         strcpy(s, str);
33         return *this;
34     }
35     mystring operator=(int i)//运算符重载,第3次
36     {
37         memset(s, 0, 1024);
38         sprintf(s, "%d", i);//把整数转化为字符串
39         return *this;
40     }
41     ~mystring()
42     {
43         cout << "~mystring" << endl;
44         delete[]s;
45     }
46 };
47 
48 void main()
49 {
50     mystring str1;
51     strcpy(str1.s, "hello, world");
52     mystring str2;
53 
54     //str2 = str1;//这个过程不是复制构造函数,而是等号=操作
55     //str2 = "test";
56     //str2 = 100;
57 
58     mystring str3;
59 
60     str3 = str2 = 100;
61 
62     cout << str2.s << endl;
63 
64     system("pause");
65 }

 

2 重载加号+

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <iostream>
 3 using namespace std;
 4 
 5 class mystring
 6 {
 7 public:
 8     char *s;
 9     mystring()
10     {
11         s = new char[1024];
12         cout << "mystring" << endl;
13     }
14     mystring(const mystring &it)//实现深拷贝
15     {
16         cout << "实现深拷贝copy string" << endl;
17         s = new char[1024];
18         memset(s, 0, 1024);
19         strcpy(s, it.s);
20         cout << "mystring" << endl;
21     }
22     mystring operator=(const mystring &it)//运算符重载,实现深拷贝,第1次
23     {
24         cout << "实现深拷贝,第1次=operator" << endl;
25         memset(s, 0, 1024);
26         strcpy(s, it.s);
27         return *this;
28     }
29     mystring operator=(const char *str)//运算符重载,第2次
30     {
31         cout << "运算符重载,第2次" << endl;
32         memset(s, 0, 1024);
33         strcpy(s, str);
34         return *this;
35     }
36     mystring operator=(int i)//运算符重载,第3次
37     {
38         cout << "运算符重载,第3次" << endl;
39         memset(s, 0, 1024);
40         sprintf(s, "%d", i);//把整数转化为字符串
41         return *this;
42     }
43     mystring operator+(const mystring &it)//运算符重载,第4次
44     {
45         cout << "运算符重载,第4次" << endl;
46         strcat(s, it.s);
47         return *this;
48     }
49     mystring operator+(const char *str)//运算符重载,第5次
50     {
51         cout << "运算符重载,第5次" << endl;
52         strcat(s, str);
53         return *this;
54     }
55     mystring operator+(int i)//运算符重载,第6次,重载了一个加号+操作符,一元操作符重载
56     {
57         cout << "运算符重载,第6次" << endl;
58         char temp[100] = { 0 };
59         sprintf(temp, "%d", i);
60         strcat(s, temp);
61         return *this;
62     }
63     ~mystring()
64     {
65         cout << "~mystring" << endl;
66         delete[]s;
67     }
68 };
69 
70 mystring operator +(const char *str, const mystring &it)
71 {
72     mystring str1;
73     char buf[1024] = { 0 };
74     sprintf(buf, "%s%s", str, it.s);
75     strcpy(str1.s, buf);
76     return str1;
77 };
78 
79 void main()
80 {
81     mystring str1;
82     strcpy(str1.s, "hello");
83     mystring str2;
84     strcpy(str1.s, " world");
85     mystring str3;
86 
87     //str3 = str1 + str2;
88 
89     //str3 = str1 + "abcdefg";
90 
91     //str3 = str1 + 100;
92 
93     str3 = "abcdefg" + str1;
94 
95     cout << str3.s << endl;
96 
97     system("pause");
98 }

 

以上是关于运算符重载的主要内容,如果未能解决你的问题,请参考以下文章

C++提高:运算符重载

导航架构片段重载问题

导航架构片段重载问题

Javascript实现运算符重载

GroovyGroovy 运算符重载 ( 运算符重载 | 运算符重载对应方法 )

运算符 + 重载 C++