重载运算符 [英] Overloading Operators

查看:95
本文介绍了重载运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在完成一项学校作业,所以请不要告诉我

解决方案。我只是想要一些方向。


我应该重载>,<,==,!=,> =和< =运算符使用

bool。我在看到需要发生的事情时遇到了一些问题

。我只是不确定我是怎么做到的。重载函数

是否会识别<通常<?我做了一个

IF(a.letters< b.letters){

返回true

}

其他{

返回false

}


??这是我需要的逻辑吗?


KL

I am working on a school assignment, so please don''t tell me the
solution. I just want some direction.

I am supposed to overload the >, <, ==, !=, >=, and <= operators using
bool. I am having a bit of a problem in seeing what needs to happen
here. I am just not sure how I do this. Will the overloading function
recognize a < and a usual <? Do I do an
IF (a.letters < b.letters){
return true
}
else {
return false
}

?? Is that the logic I need?

KL

推荐答案

KL写道:
KL wrote:
我正在完成一项学校作业,所以请不要告诉我
解决方案。我只是想要一些方向。


你不经常在这里发帖,对吧? ;-)

我应该使用
bool重载>,<,==,!=,> =和< =运算符。我在看到需要发生的事情时遇到了一些问题。


嗯,这取决于。

我不知道我是怎么做到的。重载功能是否会识别<通常<?


不确定你的意思。你的操作员只是一个功能,你可以在其中使用

所有可用于任何其他功能的东西。

我做一个
IF(a .letters< b.letters){
返回true
}
其他{
返回false
}

??这是我需要的逻辑吗?
I am working on a school assignment, so please don''t tell me the
solution. I just want some direction.
You are not posting here very often, right? ;-)
I am supposed to overload the >, <, ==, !=, >=, and <= operators using
bool. I am having a bit of a problem in seeing what needs to happen
here.
Well, that depends.
I am just not sure how I do this. Will the overloading function
recognize a < and a usual <?
Not sure what you mean. Your operator is just a function, and you can use
everything in it that you can use in any other function.
Do I do an
IF (a.letters < b.letters){
return true
}
else {
return false
}

?? Is that the logic I need?




是的,基本上。但是,你可以省略if而只是写:


返回a.letters< b.letters;


,因为'a.letters< b.letters''只是一个真正的布尔值

或false,具体取决于值。



Yes, basically. However, you can leave out the if and just write:

return a.letters < b.letters;

since the result of ''a.letters < b.letters'' is just a boolean that is true
or false, depending on the values.


Rolf Magnus写道:
Rolf Magnus wrote:
KL写道:
KL wrote:
我正在完成一项学校作业,所以请不要告诉我
解决方案。我只是想要一些方向。
I am working on a school assignment, so please don''t tell me the
solution. I just want some direction.



你不经常在这里发帖,对吧? ; - )



You are not posting here very often, right? ;-)




这是对的。我只是在遇到困难时张贴。所以基本上,不是经常。



That is right. I just post when I get stuck. So basically, not very often.

我应该重载>,<,==,!=,> =和< =运营商
使用bool。在看到这里发生什么需要时,我遇到了一些问题。
I am supposed to overload the >, <, ==, !=, >=, and <= operators
using bool. I am having a bit of a problem in seeing what needs to
happen here.



嗯,这取决于。



Well, that depends.

I我只是不确定我是怎么做到的。重载功能是否会识别<和通常的<?
I am just not sure how I do this. Will the overloading function
recognize a < and a usual <?



不确定你的意思。您的操作员只是一个功能,您可以使用其中可以用于任何其他功能的所有内容。



Not sure what you mean. Your operator is just a function, and you can
use everything in it that you can use in any other function.

我做一个
IF(a .letters< b.letters){
返回true
}
其他{
返回false
}

??这是我需要的逻辑吗?
Do I do an
IF (a.letters < b.letters){
return true
}
else {
return false
}

?? Is that the logic I need?



是的,基本上。但是,你可以省略if而只是写:

返回a.letters< b.letters;

因为'a.letters< b.letters''只是一个布尔值,它是真或假的,取决于值。



Yes, basically. However, you can leave out the if and just write:

return a.letters < b.letters;

since the result of ''a.letters < b.letters'' is just a boolean that is
true or false, depending on the values.




好​​的,所以当它被调用时,例如a< b,我的参数(已经给出了

)只传递了b。所以我的问题是如何引用一个部分。

它可以被称为任何东西。如何在我的超载

函数中引用它?


对不起,如果这看起来像一个基本问题,但就在我认为我有那个

想通了,它再次跳出我的脑袋。


-


KL

你的论点是合理的,只有声音。 -Benjamin Franklin


---- ==通过Newsfeeds.Com发布 - 无限制 - 未经审查 - 安全Usenet新闻== ----
http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组

---- =东海岸和西海岸服务器农场 - 通过加密的总隐私= ----



OK, so when it is called, for example a<b, my parameters (which were already
given) only passes the b. So my question is how to reference the a part.
It could be called anything. How do I reference it in my overloading
function?

Sorry if this seems like a basic question, but just when I think I have that
figured out, it jumps out of my head again.

--

KL

Your argument is sound, nothing but sound. -Benjamin Franklin

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


调用类似(a< b)的语句,它扩展为以下内容:

a.operator<(b);即,运营商<调用对象传递对象b

作为参数。因此你不必担心对象a(

提供a和b是相同类型/类/结构/联合的对象)。

也是你在重载的

函数中只有一个参数的原因。
when an statement like (a < b) is invoked, it expands to the following:
a.operator<(b); ie, operator < is invoked for object a passing object b
as the parameter. hence you don''t have to worry about object a (
provided a and b are objects of the same type/class/struct/union).that
is also the reason you have only one parameter in the overloaded
function.


这篇关于重载运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆