C++ 运算符重载函数
//String.h
#ifndef STRING_H
#define STRING_H
#include <iostream.h>
class String
{
friend ostream & operator<<(ostream &,const String &);
friend istream & operator>>(istream &,String &);
public:
String(const char * ="1");
String(const String &);
~String();
const String & operator=(const String &);
const String & operator+=(const String &);
bool operator!()const;
bool operator==(const String &)const;
bool operator<(const String &)const;
bool operator!=(const String & string)const
{
return !(*this==string);
}
bool operator>(const String & string)const
{
return string<*this;
}
bool operator<=(const String & string)const
{
return !(string<*this);
}
bool operator>=(const String & string)const
{
return !(*this<string);
}
char & operator[](int);
char operator[](int)const;
String operator()(int ,int =0)const;
int getLength()const;
private:
int length;
char *sptr;
void setString(const char *);
};
#endif
//String.cpp
#include <string.h>
#include <iomanip.h>
#include <cstdlib>
#include "String.h"
String::String( const char *ptr)
:length(strlen(ptr)==0?strlen(ptr):0)
{
cout<<"Conversation constructor:"<<ptr<<endl;
setString(ptr);
}
String::String(const String & string)
:length(string.length)
{
cout<<"Copy Constructor:"<<string.sptr<<endl;
setString(string.sptr);
}
String::~String()
{
cout<<"Destructor:"<<sptr<<endl;
;
}
const String & String::operator=(const String & string)
{
cout<<"operator=called"<<endl;
if (&string!=this)
{
;
length=string.length;
setString(string.sptr);
}
else
cout<<"Attempted assigment of a string to itself."<<endl;
return *this;
}
const String & String::operator+=(const String &string)
{
char temp1[100];
char temp2[100];
strcpy(temp1,sptr);
strcpy(temp2,string.sptr);
strcat(temp1,temp2);
length=length+string.length+1;
sptr=temp1;
return *this;
}
bool String::operator!() const
{
return 0==length;
}
bool String::operator==(const String & string) const
{
return 0==strcmp(sptr,string.sptr);
}
bool String::operator<(const String & string) const
{
return strcmp(sptr,string.sptr)<0;
}
char & String::operator[](int i)
{
if (i<0||i>=length)
{
cout<<"Error: i"<<i<<"out of range."<<endl;
exit(1);
}
return sptr[i];
}
char String::operator[](int i) const
{
if (i<0||i>=length)
{
cout<<"Error: i"<<i<<"out of range."<<endl;
exit(1);
}
return sptr[i];
}
String String::operator()(int start,int len) const
{
int lon;
if (start<0||start>=length||len<0)
return "";
if (0==len||(start+len)>length)
lon=length-start;
else
lon=len;
char *temper=new char [lon+1];
strncpy(temper,sptr+start,lon);
temper[lon]='\0';
String tempString(temper);
delete [] temper;
return tempString;
}
int String::getLength() const
{
return length;
}
void String::setString(const char * ptr)
{
sptr=new char [length+1];
char temp[100];
strcpy(temp,ptr);
sptr=temp;
}
ostream & operator<<(ostream & output,const String & string)
{
output<<string.sptr;
return output;
}
istream & operator>>(istream & input,String & string)
{
input>>string.sptr;
return input;
}
//main 函数
#include <iostream.h>
#include "String.h"
int main()
{
String str1("happy");
String str2(" birthday");
String str3;
cout<<"Str1 is \""<<str1<<"\";str2 is \""<<str2<<"\";str3 is \""<<str3<<'\"'<<"\n\nThe result of camparing str2 and str1 is:"
<<"\nstr2==str1 "<<(str2==str1)
<<"\nstr2!=str1 "<<(str2!=str1)
<<"\nstr2>str1 "<<(str2>str1)
<<"\nstr2<str1 "<<(str2<str1)
<<"\nstr2>=str1 "<<(str2>=str1)
<<"\nstr2<=str1 "<<(str2<=str1);
cout<<"\n\nTesting !str3:"<<endl;
if (!str3)
{
cout<<"str3 is empty;assigning str1 to str3:"<<endl;
str3=str1;
cout<<"str3 is \""<<str3<<'\"';
}
cout<<"\n\nstr1+=str2,str1=";
str1+=str2;
cout<<str1;
cout<<"\n\nstr1+=\" to you\""<<endl;
str1+=" to you";
cout<<"str1 is "<<str1<<"\n\n";
cout<<"The substring of str1 start at\n"<<"location 0 to 14 characters,str1(0,14) is :\n"<<str1(0,14)<<"\n\n";
cout<<"The substring of str1 start at\n"<<"location 15,str1(15) is :\n"<<str1(15)<<"\n\n";
String *str4=new String(str1);
cout<<"\n*str4 is "<<*str4<<"\n\n";
cout<<"assigning *str4 to *str4"<<endl;
*str4=*str4;
cout<<"*str4 is "<<*str4<<endl;
delete str4;
str1[0]='H';
str1[6]='B';
cout<<"\nstr after str1[0]='H' and str1[6]='B' is :"<<str1<<"\n\n";
cout<<"Attempt to assign 'd' to str1[30]:"<<endl;
str1[30]='d';
return 0;
}
[解决办法]
楼主什么问题
[解决办法]
void String::setString(const char * ptr)
{
sptr=new char [length+1];
char temp[100];
strcpy(temp,ptr);
sptr=temp;
}
这个函数有问题 temp是局部变量。