C++超载
Possible Duplicate:
Operator overloading
I would like to overload < and > to alphabetize strings and I'm not sure how to do it.
How do I call the string class again to redefine < and >?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要重载它们,因为
std::string
已经提供了它们。一般来说,要重载函数,您不会调用类。你不能调用一个类。您也不能向类添加方法。例如,如果您想为现有类
A
提供operator <
,则必须在A< 的命名空间中将 is 创建为自由函数/代码>:
You don't need to overload these, as
std::string
already provides them.In general, to overload functions, you don't call a class. You cannot call a class. You can also not add methods to classes. If you want to provide, for instance,
operator <
for an existing classA
, you have to create is as a free function in the namespace ofA
:二元运算符大于(>)和小于(<)运算符主要用于if语句中,因此下面的源代码示例也使用if语句。我们看一下运算符重载源码:
可以从String继承Box来为字符串重载这些运算符
The binary operators greater than (>) and less than (<) operators are mostly used in if statements, so the source code example below is also using if statements. Let's look at the operator overloading source code:
you can inherit Box from String to overload these operators for string