为什么需要重载重载的简单实现

  1 /* 运算符重载 */
  2 
  3 #include<iostream>
  4 #include<string>
  5 #include<cstring>
  6 
  7 using namespace std;
  8 
  9 int main()
 10 {
 11     
 12     char *str1 = "abc";
 13 
 14     char *str2 = "xyz";
 15 
 16     char *p = str1 + str2;// 不能相加
 17 
 18     cin.get();
 19     return 0;
 20 }
 21 
 22 //----------------------------------------------------
 23 
 24 int main()
 25 {
 26     string str1 = "hello";
 27     string str2 = "world";
 28     string str3 = str1 + str2;// 可以相加
 29     cout << str3 << endl;
 30 
 31 
 32     cin.get();
 33     return 0;
 34 }
 35 
 36 //----------------------------------------------------
 37 
 38 class mystring
 39 {
 40 public:
 41     char *p;
 42 
 43     int n;
 44 
 45     mystring():p(nullptr),n(0)// 无参构造函数
 46     {
 47     
 48     }
 49 
 50     mystring(char *str)
 51     {
 52         this->n = strlen(str)+1;// 获取长度
 53         char this->p = new char[n];// 分配内存
 54         strcpr(p,str);// 拷贝字符串
 55     }
 56     
 57     mystring(const mystring & str)// 自己实现拷贝构造  深拷贝
 58     {
 59         this->n = str.n;
 60         this->p = new char[this->n];// 分配长度
 61         strcpy(this->p,str.p);
 62     }
 63 
 64     ~mystring()
 65     {
 66         delete []p;
 67     }
 68     
 69     mystring operator+(const mystring & mystr)// 重载加法
 70     {
 71         int length =mystr.n + this->n-1;
 72         mystring tempstr;// 调用默认的构造函数
 73         tempstr.n = length;// 获取长度
 74         tempstr.p = new char[tempstr.n]// 分配内存
 75         strcpy(tempstr.p,this->p);// 拷贝
 76         strcat(tempstr.p,mystr.p);// 链接
 77         return tempstr;// 返回值有副本机制
 78     }
 79 
 80     mystring add(const mystring & mystr)
 81     {
 82         int length =mystr.n + this->n-1;
 83         mystring tempstr;// 调用默认的构造函数
 84         tempstr.n = length;// 获取长度
 85         tempstr.p = new char[tempstr.n]// 分配内存
 86         strcpy(tempstr.p,this->p);// 拷贝
 87         strcat(tempstr.p,mystr.p);// 链接
 88         return tempstr;// 返回值有副本机制
 89     }
 90 
 91 };
 92 
 93 int main()
 94 {
 95     mystring str1("hello");
 96 
 97     mystring str2("world");
 98 
 99     cout << str1.p << str2.p << endl;
100     
101     mystring str3(str1);
102 
103     cout << str3.p << endl;
104 
105     //mystring str4 = str1.add(str2);
106     
107     mystring str4 = str1 + str2;
108 
109     cout << str4.p << endl;
110 
111 
112     cin.get();
113     return 0;
114 }
115 
116 //----------------------------------------------------
117 
118 template <class T>
119 T add (T a,T b)
120 {
121     return a+b;
122 }
123 
124 int main()
125 {
126     cout << add(1,2) << endl;
127     
128     char *str1 = "123";
129 
130     char *str2 = "456";
131 
132 //    cout << add(str1,str2) << endl;
133 
134     mystring str1("hello");
135 
136     mystring str2("world");
137 
138     cout << (str1 + str2).p << endl;
139     
140     cin.get();
141     return 0;
142 }

 

posted on 2015-06-07 09:04  Dragon-wuxl  阅读(190)  评论(0)    收藏  举报

导航