读书人

关于满载+运算符

发布时间: 2013-04-02 12:35:26 作者: rapoo

关于重载+运算符
本帖最后由 rolling_in_the_deep 于 2013-03-29 22:55:03 编辑

//string2.h
#ifndef STRING_2H_
#define STRING_2H_
#include <iostream>

static int num_strings;
static const int CINLIM=80;
class String
{
char *str;
int len;
public:
String(const char *s);
String();
String(const String &st);
~String();
int length()const{return len;}

void Stringlow();
void Stringupper();
int times(char c)const;

String & operator=(const char *s);
String & operator=(const String &st);
char & operator[](int i);
const char & operator[](int i)const;
friend bool operator<(const String &s1,const String &s2);
friend bool operator>(const String &s1,const String &s2);
friend bool operator==(const String &s1,const String &s2);
friend String operator+(const String &s1,const String &s2);
//friend String operator+(const char *s1,const String &s2);

friend std::ostream & operator<<(std::ostream & os,const String &st);
friend std::istream & operator>>(std::istream & is,String &st);
static int HowMany();
};
#endif


//string2.cpp
#include "string2.h"

#include <cctype>
String::String(const char *s)
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
num_strings++;
}
String::String()
{
len=0;

str=NULL;
num_strings++;
}
String::String(const String &s)
{
len=s.len;
str=new char[len+1];
strcpy(str,s.str);
num_strings++;
}
String::~String()
{
delete []str;
--num_strings;
}

String & String::operator=(const String &s)
{
if(this==&s)
return *this;
delete []str;
len=s.len;
str=new char[len+1];
strcpy(str,s.str);
return *this;
}
String & String::operator=(const char *s)
{
delete []str;
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
return *this;
}

char & String::operator[](int i)
{
return str[i];

}
const char & String::operator[](int i)const
{
return str[i];
}
bool operator<(const String &s1,const String &s2)
{
return (strcmp(s1.str,s2.str)<0);
}
bool operator>(const String &s1,const String &s2)
{
return (s2<s1);
}
bool operator==(const String &s1,const String &s2)
{
return (strcmp(s1.str,s2.str)==0);
}

std::ostream & operator<<(std::ostream &os,const String &s)
{
os<<s.str;
return os;
}
std::istream & operator>>(std::istream &is,String &s)
{
char temp[CINLIM];
is.get(temp,CINLIM);
if(is)
s=temp;
while(is&&is.get()!='\n')
continue;
return is;



}

String operator+(const String &s1,const String &s2)
{

int len=s1.len+s2.len;
char *p=new char[len+1];
strcpy(p,s1.str);
strcat(p,s2.str);
String temp(p);
return temp;

}
/*String operator+(const char *s1,const String &s2)
{
int len=strlen(s1)+s2.len;
char *p=new char[len+1];
strcpy(p,s1);
strcat(p,s2.str);
String temp(p);
return temp;
}*/
void String::Stringlow()
{
for(int i=0;i<len;i++)
str[i]=tolower(str[i]);

}
void String::Stringupper()
{
for(int i=0;i<len;i++)
str[i]=toupper(str[i]);
}
int String::times(char c)const
{
int times=0;
for(int i=0;i<len;i++)
if(str[i]==c)
times++;
return times;

}


//usestring2.cpp
#include "string2.h"
using namespace std;
int main()
{
String s1("and i ma a c++ student");
String s2=("Please enter your name:");
String s3;
cout<<s2;
cin>>s3;
s2="My name is"+s3
cout<<s2<<".\n";
s2=s2+s1;
s2.Stringupper();
cout<<"The string\n"<<s2<<"\ncontains"<<s2.times('A')
<<"'A' characters in it.\n";
s1="red";
String rgb[3]={String(s1),String("green"),String("blue")};
cout<<"Enter the name of a primary color for mixing light:";
String ans;
bool success=false;
while(cin>>ans)
{
ans.Stringlow();
for(int i=0;i<3;i++)
{
if(ans==rgb[i])
{
cout<<"That's right!\n";
success=true;
break;
}
}
if(success)
break;
else cout<<"Try again\n";

}
cout<<"Bye\n";
return 0;
}

[解决办法]
对滴。如果没有使用explicit,而且没有超过转换次数上限,就可以隐式转换。
关于后面那点,还是用具体代码说明吧:

class Arg { public: Arg(int i = 0) {} };
class A { public: A(Arg) {} };
class B { public: B(Arg) {} };
// no operator+ for Arg
const A operator+(const A& v1, const B& v2) { return v1; }

Arg v1, v2;
A a(v1);
B b(v2);
a + v2;
v1 + b;
a + b;
v1 + v2; // 前面这些都可以
a + 1;// 错误,编译器只会隐式转换一次,不会从int->Arg->B,然后调用A + B

读书人网 >C++

热点推荐