你好, 在看到一些关于运算符重载的例子后,我仍然对一般语法感到困惑。以下是我的想法,而不是 确定它是否正确。 1.对于一个一元运算符,它是一个类的成员,它的形式是 通常 " operatorP()" (其中P是运营商的名字)。 如果它是非会员运营商,那么它的表格是 operatorP(arg) 2.对于一个二元运算符,它是一个类的成员,其形式为: operatorP(arg), 其中arg是P的正确参数。 如果它是非会员运营商,那么它的形式是 " operatorP(argLeft,argRight)" 有什么我错过的吗?此外,我可以超载三元 运算符吗?我认为只有一个三元运算符,即a? b:c。 非常感谢, Jess
解决方案* Jess: > 看到一些关于运算符重载的例子之后,我仍然对一般语法感到困惑。以下是我的想法,而不是 确定它是否正确。 1.对于一个一元运算符,它是一个类的成员,它的形式是 通常 " operatorP()" (其中P是运营商的名字)。 如果它是非会员运营商,那么它的表格是 operatorP(arg) 2.对于一个二元运算符,它是一个类的成员,其形式为: operatorP(arg), 其中arg是P的正确参数。 如果它是非会员运营商,那么它的形式是 " operatorP(argLeft,argRight)" 有什么我错过的吗?
对于operator ++和operator--你通过一个虚拟int第二个参数来区分前缀和 后缀形式。
此外,我可以重载三元运算符吗?No.
我认为只有一个三元运算符,是a? B:C" ;.是的。 - 答:因为它弄乱了订单人们通常会阅读文字。 问:为什么这么糟糕? A:热门帖子。 问:什么是在usenet和电子邮件中最烦人的事情是什么?
Jess写道: 看了一些关于运算符重载,我仍然有点混淆了一般语法。以下是我的想法,而不是 确定它是否正确。 1.对于一个一元运算符,它是一个类的成员,它的形式是 通常 " operatorP()" (其中P是运营商的名字)。
在文献中你会发现''''用来代替''''。
如果它是非会员运营商,那么它的表格是 " operatorP(arg)" 2.对于一个二元运算符,它是一个类的成员,它的形式是: operatorP(arg), 其中arg是P.的正确论据。 如果它是非会员运营商,那么它的形式是 " operatorP(argLeft ,argRight)" 有什么我错过的吗?不是。重载运算符也可以使用const参数, 所以当它是成员时,它可以有''rv operator @()const''。
此外,我可以重载三元 运算符吗?No.你也不能超载''。''(点),也不能''sizeof''。你也可以可能不应该超逻辑OR和逻辑AND(你可以,然而, )。
我认为只有一个三元运算符,即a? b:c。正确。 V - 请在通过电子邮件回复时删除资本''A' 我没有回复最热门的回复,请不要问
Victor Bazarov写道: 另外你 可能不应该超载逻辑OR和逻辑AND(你可以, 但是)。
这是一个建议,因为最常见的情况是你b $ b可能想要超载||和&&是支持这样的表达式: MyClass a,b; ... if(a || b)。 .. 这样做更好,而不是在''MyClass'中定义一个operator bool() ?
Hello, After seeing some examples about operator overloading, I''m still a bit confused about the general syntax. The following is what I think, not sure whether it''s correct. 1. For a unary operator that''s a member of a class, its form is usually "operatorP()" (where P is the operator''s name). If it''s a non-member operator, then its form is "operatorP(arg)" 2.For a binary operator that''s a member of a class, its form is: operatorP(arg), where arg is the right argument of P. If it''s a non-member operator, then its form is "operatorP(argLeft,argRight)" Is there anything I''ve missed? Moreover, can I overload a ternary operator? I think there''s only one ternary operator, which is "a ? b:c". Many thanks, Jess
解决方案 * Jess: > After seeing some examples about operator overloading, I''m still a bit confused about the general syntax. The following is what I think, not sure whether it''s correct. 1. For a unary operator that''s a member of a class, its form is usually "operatorP()" (where P is the operator''s name). If it''s a non-member operator, then its form is "operatorP(arg)" 2.For a binary operator that''s a member of a class, its form is: operatorP(arg), where arg is the right argument of P. If it''s a non-member operator, then its form is "operatorP(argLeft,argRight)" Is there anything I''ve missed?For operator++ and operator-- you differentiate between prefix and postfix forms via a dummy int second argument.
Moreover, can I overload a ternary operator?No.
I think there''s only one ternary operator, which is "a ? b:c".Yes. -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail?
Jess wrote: After seeing some examples about operator overloading, I''m still a bit confused about the general syntax. The following is what I think, not sure whether it''s correct. 1. For a unary operator that''s a member of a class, its form is usually "operatorP()" (where P is the operator''s name).In literature you''ll find ''@'' is used instead of ''P''.
If it''s a non-member operator, then its form is "operatorP(arg)" 2.For a binary operator that''s a member of a class, its form is: operatorP(arg), where arg is the right argument of P. If it''s a non-member operator, then its form is "operatorP(argLeft,argRight)" Is there anything I''ve missed?Not really. Overloaded operators can take const arguments as well, so when it''s a member, it can have the form ''rv operator@() const''.
Moreover, can I overload a ternary operator?No. Neither can you overload ''.'' (dot), nor ''sizeof''. Also you probably shouldn''t overload logical OR and logical AND (you may, however).
I think there''s only one ternary operator, which is "a ? b:c".Right. V -- Please remove capital ''A''s when replying by e-mail I do not respond to top-posted replies, please don''t ask
Victor Bazarov wrote: Also you probably shouldn''t overload logical OR and logical AND (you may, however).Is this a recommendation because the most common case where you might want to overload || and && is to support expressions like: MyClass a, b; ... if(a || b) ... and this is better done by instead defining an "operator bool()" in ''MyClass''?
更多推荐
重载运算符的语法
发布评论