| 查看: 309 | 回复: 1 | ||
cw277木虫 (正式写手)
|
[求助]
一个c++程序 重载的问题、、
|
|
不用友元函数,怎么去重载这个大于号? #include #include 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< 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)< [ Last edited by cw277 on 2012-9-12 at 23:09 ] |
» 猜你喜欢
26/27申博自荐
已经有10人回复
东北林业大学材料科学与工程学院“一流”A+学科国家级人才团队课题组招收2026级博士生
已经有3人回复
医学类期刊求推荐
已经有5人回复
生活琐事由它去
已经有4人回复
提交了我也来说说感想
已经有12人回复
青B发送上会通知了吗
已经有9人回复
西安交大新媒学院副院长用撤稿论文结题
已经有6人回复
论文撤稿了
已经有8人回复
化学专业申博
已经有4人回复
某211大学教师把个人教师官方主页改成:我跑了我跑了我跑了!官宣跑路!
已经有5人回复
» 本主题相关价值贴推荐,对您同样有帮助:
C++ Cookbook 中文清晰版》(清华大学出版)【已搜索无重复】
已经有91人回复
VC++中函数返回数组指针或者带指针的结构体的编译方式是否可取?
已经有6人回复
复制构造函数问题
已经有14人回复
【分享】数据结构算法与应用 C++语言描述
已经有9人回复

【答案】应助回帖
★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★
感谢参与,应助指数 +1
cw277: 金币+20, ★★★★★最佳答案, 早点来嘛, 我都解决了才来~ 2012-09-13 14:55:54
xzhdty: 谢谢 2012-09-13 15:44:59
感谢参与,应助指数 +1
cw277: 金币+20, ★★★★★最佳答案, 早点来嘛, 我都解决了才来~ 2012-09-13 14:55:54
xzhdty: 谢谢 2012-09-13 15:44:59
|
用成员函数的话,由于在隐性传递了this指针,所以已经有了一个参数,对 operator> 进行重载,只需要再加上另外一个参数即可。 修改后代码: #include #include using namespace std; class String { public: String( ){p=NULL;} String(char *str); bool operator>(String& string1); // 这里不用友元,我想用成员函数 void display( ); private: char *p; //字符型指针,用于指向字符串 }; String::String(char *str) {p=str;} void String::display( ) //输出p所指向的字符串 {cout< bool String:: operator > (String &string1) //定义运算符重载函数 { if(strcmp(p,string1.p)>0) return true; else return false; } void main() { String string1("Hello" ) ,string2("Book" ) ; cout<<(string1 > string2)< |
2楼2012-09-13 12:27:35












回复此楼