以下是string类的代码:
string.h代码如下:
#include<iostream>
using namespace std;
#ifndef STRING_H
#define STRING_H
//-------------------------------------------------------------------
class String
{
private:
char* m_pData;
public:
String(const char* pstr=NULL);
String(const String& obj);
String& operator=(const String& obj);
String& operator=(const char* pstr);
String& operator=(int nNum);
char& operator[](int nIdx);
const char& operator[](int nIdx)const;
String& operator[](const String& obj);
String& operator+=(const String& obj);
String& operator+=(const char* pstr);
int operator!=(const String&obj);
~String();
};
#endif
以下是主函数:
#include"string.h"
#include <cstdlib>
#include <iostream>
using namespace std;
int String::operator!=(const String&obj)
{
return strcmp(m_pData,obj.m_pData);
/*int j=0;
int i=0;
int m_pDataLength=strlen(m_pData);
int objLength=strlen(obj.m_pData);
if(m_pDataLength!=objLength)
{
cout<<"there are not equal"<<endl;
return 0;
}
else
{
for(i=0;i<m_pDataLength;i++,j++)
{
if(m_pData!=obj.m_pData)
break;
}
cout<<"there are equal"<<endl;
return 1;
}
*/
}
String::~String()
{
delete[]m_pData;
m_pData=NULL;
}
//-------------------------------------------------------------------
String& String::operator+=(const char* pstr)
{
if(pstr==NULL)
{
return *this;
}
char *pTmp = new char[strlen(m_pData) + strlen(pstr) + 1];
strcpy(pTmp, m_pData);
strcat(pTmp, pstr);
delete []m_pData;
m_pData = pTmp;
return *this;
}
//-------------------------------------------------------------------
String& String::operator+=(const String& obj)
{
if(obj.m_pData==NULL)
{
return *this;
}
char *pTmp = new char[strlen(m_pData) + strlen(obj.m_pData) + 1];
strcpy(pTmp, m_pData);
strcat(pTmp, obj.m_pData);
delete []m_pData;
m_pData = pTmp;
return *this;
}
//-------------------------------------------------------------------
String& String::operator[](const String& obj)
{
if (obj.m_pData == NULL)
{
return *this;
}
char *pTmp = new char[strlen(m_pData) + strlen(obj.m_pData) + 1];
strcpy(pTmp, m_pData);
strcat(pTmp, obj.m_pData);
delete []m_pData;
m_pData = pTmp;
return *this;
}
//-------------------------------------------------------------------
const char& String::operator[](int nIdx)const
{
return m_pData[nIdx];
}
//-------------------------------------------------------------------
char& String::operator[](int nIdx)
{
return m_pData[nIdx];
}
//-------------------------------------------------------------------
String& String::operator=(int nNum)
{
char szNum[24];
itoa(nNum, szNum, 10);
delete []m_pData;
m_pData = new char[strlen(szNum)+1];
strcpy(m_pData, szNum);
return *this;
}
//-------------------------------------------------------------------
String& String::operator=(const char* pstr)
{
if(pstr==m_pData)
{
return *this;
}
delete[] m_pData;
m_pData=new char[strlen(pstr)+1];
strcpy(m_pData,pstr);
return *this;
}
//-------------------------------------------------------------------
String& String::operator=(const String& obj)
{
if(&obj==this)
{
return *this;
}
delete[] m_pData;
m_pData=new char[strlen(obj.m_pData)+1];
strcpy(m_pData,obj.m_pData);
return *this;
}
//-------------------------------------------------------------------
String::String(const String& obj)
{
m_pData=new char[strlen(obj.m_pData)+1];
strcpy(m_pData,obj.m_pData);
};
//-------------------------------------------------------------------
String::String(const char* pstr/*=NULL*/)
{
if(NULL==pstr)
{
m_pData=new char[1];
*m_pData='\0';
}
else
{
m_pData=new char[strlen(pstr)+1];
strcpy(m_pData,pstr);
}
}
//-------------------------------------------------------------------------
int main( )
{
char arr[9]={'q','f','c','d','b','3','e','d','o'};
char* a=arr;
String str1(a);
String str2(arr);
if(str1!=str2)
cout<<"there are not equal"<<endl;
else
cout<<"there are equal"<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
上述程序在DEVC++中可以编译运行,没有任何问题。
可在VC++2010中却有如下警告或错误:
来源:oschina
链接:https://my.oschina.net/u/181847/blog/42485