首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

不用友元函数,如何去重载这个大于号

2012-09-20 
不用友元函数,怎么去重载这个大于号?不用友元函数,怎么去重载这个大于号?#include iostream#include st

不用友元函数,怎么去重载这个大于号?
不用友元函数,怎么去重载这个大于号?


#include <iostream>
#include <string>
using namespace std;

class String
{public:
String( ){p=NULL;}
String(char *str);
bool operator>(String &string1,String &string2);// 这里不用友元,我想用成员函数
void display( );
private:
char *p; //字符型指针,用于指向字符串
};
String::String(char *str)
{p=str;}

void String::display( ) //输出p所指向的字符串
{cout<<p;}

bool String:: operator > (String &string1,String &string2) //定义运算符重载函数
{
  if(strcmp(string1.p,string2.p)>0)
  return true;
  else return false;
}

void main()
{
  String string1("Hello" ) ,string2("Book" ) ;
  cout<<(string1>string2)<<endl;
}



[解决办法]
声明改为:

C/C++ code
bool operator>(String &string); 

热点排行