473,657 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

syntax for overloading operators

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(argL eft,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

May 21 '07 #1
19 2289
* 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(argL eft,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?
May 21 '07 #2
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(argL eft,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
May 21 '07 #3
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'?
May 21 '07 #4
Juha Nieminen wrote:
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'?
I don't know what the most common case in which you might want to
overload || and &&. Honestly, I don't. I only know that if those
are overloaded, the logic essentially changes -- no more "short
circuit" evaluation.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 21 '07 #5

"Juha Nieminen" <no****@thanks. invalidwrote in message
news:46******** **************@ news.song.fi...
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'?
IMHO the reason is the confusion that overloaded &&/|| operators cannot
shortcut: to pass arguments to the overloaded operation the compiler need to
evaluate both parameters, and this can be confusing if you expect that b is
not evaluated if a has some specific property...

--
Marco
May 21 '07 #6

"*PaN!*" <sp***@pinningi ds.orgwrote in message
news:FR******** *********@torna do.fastwebnet.i t...
IMHO the reason is the confusion that overloaded &&/|| operators cannot
shortcut: to pass arguments to the overloaded operation the compiler need
to evaluate both parameters, and this can be confusing if you expect that
b is not evaluated if a has some specific property...
I should have reread the sentence... ok, as Victor said, is that you no
longer have shortcut evaluation ;)

--
Marco
May 21 '07 #7
On 2007-05-21 06:47:40 -0700, Juha Nieminen <no****@thanks. invalidsaid:
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'?
I would actually recommend against writing an "operator bool()" in
almost any circumstance. With the implicit conversion from bool to any
arithmetic type, some counterintuitiv e (at first glance) conversions
will be allowed by the compiler.
--
Clark S. Cox III
cl*******@gmail .com

May 21 '07 #8
Clark Cox wrote:
On 2007-05-21 06:47:40 -0700, Juha Nieminen <no****@thanks. invalid>
said:
>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'?

I would actually recommend against writing an "operator bool()" in
almost any circumstance. With the implicit conversion from bool to any
arithmetic type, some counterintuitiv e (at first glance) conversions
will be allowed by the compiler.
To Juha:

.... and as a fairly common replacement for 'operator bool()' you might
consider 'operator void*()' which would return a null pointer in case
of 'false' and "true" otherwise (don't return 'this', others might
figure it out and try taking advantage of it).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 21 '07 #9
Clark Cox wrote:
I would actually recommend against writing an "operator bool()" in
almost any circumstance. With the implicit conversion from bool to any
arithmetic type, some counterintuitiv e (at first glance) conversions
will be allowed by the compiler.
I thought the C++ standard forbids two implicit conversions to be
performed on the same type.

In other words, if you have a class A with an operator bool() defined,
this is valid:

A a;
bool b = a;

but this is not (and should give a compiler error):

A a;
int b = a;

because it would require *two* implicit conversions.

Thus I don't really see the problem you are referring to.
May 21 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
33863
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
13
2003
by: denis wendum | last post by:
In a nutshell: What is the equivalent of __radd__ (wich overloads the right hand side of +) when overloading the comparison operators <,>,== and so on. My first guess __rlt__ for overloading the rigth hand side of < did not work. Expanded version: In a class of numbers I have been able to overload the four arithmetic operators +,-,* and / by defining the methods __add__ and __radd__ (etc..) in my class. This enables me to evaluate...
7
4375
by: Petr Prikryl | last post by:
Hi, Summary: In my opinion, the C-like prefix increment and decrement operators (++i and --i) should be marked as "syntax error". Current situation: try... (Python 2.4 (#60, ...)) >>> i = 1 >>> i
2
2270
by: bq | last post by:
Hello, This post is really two questions. Question 1: What is the current status on a revision of ISO C++, specifically regarding plans for overloading composite operators? Some people in this group probably would know. By "overloading composite operators" I mean conversion of an expression like A = B * C; into a single function call (instead of three calls; one to "*", another to copy and another to "="). Here A, B and C are of a
20
1829
by: KL | last post by:
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 }
4
2617
by: jelle | last post by:
Hi, I use python quite a bit to couple different programs together. Doing so has been a _lot_ easier since subprocess came around, but would really like to be able to use the succinct shell syntax; >, <, | That really shouldn't be too hard to wrap in a class, but so far I didn't succeed to do so this well, since I'm facing some trouble with operator precedence that I do not know how to overcome.
5
3619
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), , ->, =. I wonder why there is such a restriction. Some tutorials say that 'new' and 'delete' can only be overloaded with static member functions, others say that all overloading function should be non-static. Then what is the fact, and why? ...
15
1736
by: PengYu.UT | last post by:
Hi, It seems that C++ does not allow overloading operators for primative types, e.g. int, double. I'm wondering whether it is ture or there is some walk-around? Thanks, Peng #include <iostream>
8
2968
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that these operators must be nonstatic member functions, but why this ensure their first operands will...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8826
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.