string类的实现:在VS2010和DEV4.9.9.2之间的差异

折月煮酒 提交于 2019-12-09 11:40:46

以下是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中却有如下警告或错误:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!