欢迎C++同学前来帮忙!

wjxluoye 2014-09-24 09:07:57
欢迎大师前来帮助!
#include <iostream>
using namespace std;
class String
{
public:
String(){ p = NULL; }
String(char *str);
friend bool operator >(String &string1, String &string2);
friend bool operator <(String &string1, String &string2);
friend bool operator ==(String &string1, String &string2);
void display();
private:
char *p;
};
String::String(char *str)
{
p = str;
}
void String::display()
{
cout << p;
}
bool operator >(String &string1, String &string2)
{
if (strcmp(string1.p, string2.p) > 0)
return true;
else
return false;
}
bool operator <(String &string1, String &string2)
{
if (strcmp(string1.p, string2.p) < 0)
return true;
else
return false;
}
bool operator ==(String &string1, String &string2)
{
if (strcmp(string1.p, string2.p) == 1)
return true;
else
return false;
}
void compare(String &string1, String &string2)
{
if (operator >(string1, string2) == 1)
{
string1.display(); cout << ">"; string2.display();
}
else
if (operator <(string1, string2) == 1)
{
string1.display(); cout << "<"; string2.display();
}
else
if (operator ==(string1, string2) == 1)
{
string1.display(); cout << "="; string2.display();
}
cout << endl;
}
int main()
{
String string1("Hello"), string2("Book"), string3("Computer"), string4("Hello");
compare(string1, string2);
compare(string2, string3);
compare(string1, string4);
return 0;
}

这是一个简单地C++程序,我想问一下
friend bool operator >(String &string1, String &string2);
friend bool operator <(String &string1, String &string2);
friend bool operator ==(String &string1, String &string2);
void display();


里面的String &string1, String &string2
是什么意思?为什么要这样?
...全文
322 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
百曉生 2014-09-29
  • 打赏
  • 举报
回复
QIUSQJF 2014-09-26
  • 打赏
  • 举报
回复
引用,就有点像在C中传指针;传入引用,则不会进行副本拷贝,效率高,而且是真实的比较这两个数,而如果没有这个引用,这是这两个数的副本在比较……

13,824

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧