求简化 求简化第一个+符号重载
#include<iostream>
#include<string.h>
using namespace std;
class CString
{
public:
char * m_sChars;
public:
CString(char *sChars = "" ) //构造函数
{
cout<<"gouzao"<<endl;
m_sChars = new char [strlen(sChars) + 1];
strcpy(m_sChars,sChars);
}
CString(CString &TCString)//拷贝构造函数
{
cout<<"kaobei"<<endl;
m_sChars = new char [strlen(TCString.m_sChars) + 1];
strcpy(m_sChars,TCString.m_sChars);
}
~CString()//析构函数
{
cout<<m_sChars<<" 被删除!"<<endl;
if(m_sChars != NULL)
{
delete [] m_sChars;
}
}
CString &operator = (CString &CTemp);//=的重载
CString &operator = (char * sChars);//=的重载
CString &operator + (CString &CTemp);//+的重载
//CString operator + (const char * sChars);//+的重载
int GetLength()
{
return strlen(m_sChars);
}
void Show()
{
cout<<m_sChars<<endl;
}
};
CString &CString::operator =(CString &CTemp)//CString &CTemp = 重载的实现 str2 = str1
{
cout<<"重载的实现 str2 = str1"<<endl;
if(this == &CTemp)
return *this;
delete [] this->m_sChars;
this->m_sChars = new char [strlen(this->m_sChars)+1];
strcpy(this->m_sChars , CTemp.m_sChars);
return *this;
}
CString &CString::operator = ( char * sChars)// char * sChars = 重载的实现 str1 = "everyone"
{
cout<<"重载的实现 str1 = 'everyone'"<<endl;
this->m_sChars = new char [strlen (sChars) + 1];
strcpy(this->m_sChars,sChars);
return *this;
}
CString &CString::operator + ( CString &CTemp) //+重载的实现 str3 = str1 + str2 :str3 = str1.operator+(str2)
{
cout << "+" << endl;
char *temp = new char[strlen(this->m_sChars) + 1 + strlen(CTemp.m_sChars)];
strcpy(temp,this->m_sChars);
delete[] this->m_sChars;
this->m_sChars = new char[strlen(temp) + 1 + strlen(CTemp.m_sChars)];
strcat(temp,CTemp.m_sChars);
strcpy(this->m_sChars, temp);
delete[] temp;
return *this;
}
//CString CString::operator + (const char * sChars)// +的重载
//{
//if (this->m_sChars != NULL)
//{
//strcat(this->m_sChars, sChars);
//}
//else
//{
//this->m_sChars = new char [strlen (sChars) + 1];
//strcpy(this->m_sChars, sChars);
//}
//return (*this);
//}
void main()
{
CString str1 = "everyone,";
CString str2 = "C++";
//str2 = str1;
//str2.Show();
//str2 = "C++";
//str2.Show();
CString str3 = str1 + str2 ;
//CString str3 = "Hello!" + str1 + "welcome to " + str2 + " world!";
str3.Show();
//str1[0] = 'E';
//str3[5] = ',';
//cout << str3 << endl;
//str3 = "12345";
//
//if (str3.GetLength() > 0)
//{
//CString str4 = str3;
//cout << atoi(str4) << endl;
//}
}
[解决办法]
- C/C++ code
CString &CString::operator + ( CString &CTemp) //+重载的实现 str3 = str1 + str2 :str3 = str1.operator+(str2){cout << "+" << endl;char *temp = new char[strlen(this->m_sChars) + 1 + strlen(CTemp.m_sChars)];strcpy(temp,this->m_sChars);//delete[] this->m_sChars;//this->m_sChars = new char[strlen(temp) + 1 + strlen(CTemp.m_sChars)];strcat(temp,CTemp.m_sChars);//strcpy(this->m_sChars, temp);//delete[] temp;m_sChars = temp;return *this;}
------解决方案--------------------
这程序写的一点也不规范,申请内存不要加保护吗
char *temp = new char[strlen(this->m_sChars) + 1 + strlen(CTemp.m_sChars)];
this->m_sChars = new char[strlen(temp) + 1 + strlen(CTemp.m_sChars)];
长度分配重复,用int len = strlen(this->m_sChars) + 1 + strlen(CTemp.m_sChars);
char *temp = new char[len];
this->m_sChars = new char[len];
这样效率高些,也符合企业规范