473,795 Members | 2,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overloading of ","

Hi, I coded the following but It does not return what I expect, why?

#include <iostream>

using namespace std;

class Other
{
public:
int i;

Other(int x=1)
{
i = x;
}

Other *operator-() { return this;}

Other &operator+ (Other t)
{
i += t.i;

return *this;
}

Other &operator,(Othe r oth)
{
i = oth.i;

return *this;
}

};

int main()
{
Other o0, o1, o2(4), o3(5);

o0->i = 100;

cout << o0.i << "\n" << o0->i << "\n";

// HERE it returns 5 AND not 6 WHY ??????????????? ????
Other ox = (o1 + o1, o3 = o2 + o1);
// ------------------
cout << ox.i << endl;

return 0;
}

Mar 12 '07
35 2072
red floyd a écrit :
josh wrote:
>On 13 Mar, 04:28, red floyd <no.s...@here.d udewrote:
>>Adrian Hawryluk wrote:
Old Wolf wrote:
To answer OP's question: the behaviour is unspecified because
overloade d comma operator does not retain the ordering of the
builtin comma operator. What's happening in this case is that
(o3 = o2 + o1) is being executed, and then (o1 + o1), and then
the overloaded comma operator is called with both of those results.
A different compiler might evaluate (o1 + o1) first, resulting in
o5 == 6.
So does that mean that the comma operator's evaluation order of left to
right is not implemented correctly on the compiler? I thought that it
was defined as left to right.
It is, if you don't overload it. If you overload it, then the
evaluation order is undefined, as I understand it. Similarly, if you
overload && or ||, the "short-circuit" behavior does not occur.
have you read it on the 14482 iso spec.?

I believe so. I'm at home right now, and my copy of 14882 is at work,
so it'll have to wait, unless other posters wish to comment on that.
[1.9/18] The POD &&, ||, ?: and , define a sequence.
In footnote (12) of the paragraph, it is specified that when those
operators are overloaded, they designate a function and "the operand
form an argument list without an implied sequence point between them".
Michael
Mar 14 '07 #21
On 14 Mar, 10:16, Michael DOUBEZ <michael.dou... @free.frwrote:
red floyd a écrit :
josh wrote:
On 13 Mar, 04:28, red floyd <no.s...@here.d udewrote:
Adrian Hawryluk wrote:
Old Wolf wrote:
To answer OP's question: the behaviour is unspecified because
overloaded comma operator does not retain the ordering of the
builtin comma operator. What's happening in this case is that
(o3 = o2 + o1) is being executed, and then (o1 + o1), and then
the overloaded comma operator is called with both of those results.
A different compiler might evaluate (o1 + o1) first, resulting in
o5 == 6.
So does that mean that the comma operator's evaluation order of leftto
right is not implemented correctly on the compiler? I thought that it
was defined as left to right.
It is, if you don't overload it. If you overload it, then the
evaluation order is undefined, as I understand it. Similarly, if you
overload && or ||, the "short-circuit" behavior does not occur.
have you read it on the 14482 iso spec.?
I believe so. I'm at home right now, and my copy of 14882 is at work,
so it'll have to wait, unless other posters wish to comment on that.

[1.9/18] The POD &&, ||, ?: and , define a sequence.
In footnote (12) of the paragraph, it is specified that when those
operators are overloaded, they designate a function and "the operand
form an argument list without an implied sequence point between them".

Michael
so when am I overloading that operators the left-to-right sequence is
not implied (guaranted) and
if I have i.e. a && b && c (where a,b,c are object of a Type class
with that operators overloaded) is not evaluated first a && b and then
the result with c ???

Mar 14 '07 #22
josh a écrit :
On 14 Mar, 10:16, Michael DOUBEZ <michael.dou... @free.frwrote:
[snip]
>[1.9/18] The POD &&, ||, ?: and , define a sequence.
In footnote (12) of the paragraph, it is specified that when those
operators are overloaded, they designate a function and "the operand
form an argument list without an implied sequence point between them".

so when am I overloading that operators the left-to-right sequence is
not implied (guaranted) and
if I have i.e. a && b && c (where a,b,c are object of a Type class
with that operators overloaded) is not evaluated first a && b and then
the result with c ???
No. Suposing the operator&& resolves to binary function, your expression
will be
operator&&(oper ator&&(a,b),c);

Then a,b and c will be evaluated in any order.

Michael
Mar 14 '07 #23
On 14 Mar, 10:51, Michael DOUBEZ <michael.dou... @free.frwrote:
josh a écrit :
On 14 Mar, 10:16, Michael DOUBEZ <michael.dou... @free.frwrote:
[snip]
[1.9/18] The POD &&, ||, ?: and , define a sequence.
In footnote (12) of the paragraph, it is specified that when those
operators are overloaded, they designate a function and "the operand
form an argument list without an implied sequence point between them".
so when am I overloading that operators the left-to-right sequence is
not implied (guaranted) and
if I have i.e. a && b && c (where a,b,c are object of a Type class
with that operators overloaded) is not evaluated first a && b and then
the result with c ???

No. Suposing the operator&& resolves to binary function, your expression
will be
operator&&(oper ator&&(a,b),c);

Then a,b and c will be evaluated in any order.

Michael
yes but the calling function operator () came before any other
operator and in that case
will be logic if the compiler first called operator&&(a,b) and then
comapared the result with c

Josh

Mar 14 '07 #24
josh a écrit :
On 14 Mar, 10:51, Michael DOUBEZ <michael.dou... @free.frwrote:
>josh a écrit :
>>On 14 Mar, 10:16, Michael DOUBEZ <michael.dou... @free.frwrote:
[snip]
[1.9/18] The POD &&, ||, ?: and , define a sequence.
In footnote (12) of the paragraph, it is specified that when those
operators are overloaded, they designate a function and "the operand
form an argument list without an implied sequence point between them".
so when am I overloading that operators the left-to-right sequence is
not implied (guaranted) and
if I have i.e. a && b && c (where a,b,c are object of a Type class
with that operators overloaded) is not evaluated first a && b and then
the result with c ???
No. Suposing the operator&& resolves to binary function, your expression
will be
operator&&(ope rator&&(a,b),c) ;

Then a,b and c will be evaluated in any order.

Michael

yes but the calling function operator () came before any other
operator and in that case
will be logic if the compiler first called operator&&(a,b) and then
comapared the result with c
Google for Sequence Point.

Michael
Mar 14 '07 #25
josh wrote:
>Precedence and arity do match. I have no idea, what you mean by
associativit y.

when I say about associativity I mean that if we have in an expression
more operators
of equal precedence i.e. * / % than it will be evaluated from left-to-
rigth
int a = 4 / 2 * 8 -first 4/2 and SECOND 2 * 8
I see. Note, however, that this provision does not reflect the evaluation
order in terms of which subexpressions get evaluated first. E.g.,

a + b - c * e

by left-to-right grouping for + and - is equivalent to

( a + b ) - ( c * e )

The compiler, however, is free to have c*e evaluated before a+b.

if I add to my class an overload of * and than I make i.e.
o1 + o2 * 03 the evaluation order is correct and in fact will be FIRST
o2 * o3 and SECOND

This is false: the evaluation order is unspecified. This is unrelated to
overloading. It is unspecified for built-in types, as well. See clause
[5/4].
This is not false! the evaluation order is the same of PDO. It is
unspecified
only if I have in an expression the same operators i.e.
Nope: in a*b-d/c it is unspecified whether a*b is evaluated before or after
d/c.
int a = 4 + 6 + b + f -here is not sure that the compiler goes from
left to right
it is guaranted only for && !! , ?: operators

Best

Kai-Uwe Bux
Mar 14 '07 #26
Nope: in a*b-d/c it is unspecified whether a*b is evaluated before or after
d/c.

yes for a*b-d/c but we are speaking about more complex expressions
where
the order is: precedence and associativity but however I think that
the
comma operator when we overload it don't respect the PDO rules where
is guaranted the left-to-right evaluation (as && || and ?:)

Josh

Mar 14 '07 #27
Michael DOUBEZ wrote:
red floyd a écrit :
[discussion on overloaded &&, ||, and ',' as sequence points redacted]
>>>

I believe so. I'm at home right now, and my copy of 14882 is at work,
so it'll have to wait, unless other posters wish to comment on that.

[1.9/18] The POD &&, ||, ?: and , define a sequence.
In footnote (12) of the paragraph, it is specified that when those
operators are overloaded, they designate a function and "the operand
form an argument list without an implied sequence point between them".
Thanks, Michael. I missed that one. I was looking at 13.6 and couldn't
find it.
Mar 14 '07 #28
josh wrote:
>
>Nope: in a*b-d/c it is unspecified whether a*b is evaluated before or
after d/c.


yes for a*b-d/c but we are speaking about more complex expressions
where
the order is: precedence and associativity but however I think that
the
comma operator when we overload it don't respect the PDO rules where
is guaranted the left-to-right evaluation (as && || and ?:)
Exactly, the overloaded comma-operator differs from the comma-operator for
POD types in this regard. And, I already said that:
Anyhow, what an overloaded comma operator lacks is the
sequence point that the built-in comma operator gives you.

Best

Kai-Uwe Bux
Mar 14 '07 #29
josh wrote:
>Nope: in a*b-d/c it is unspecified whether a*b is evaluated before or after
d/c.


yes for a*b-d/c but we are speaking about more complex expressions
where
the order is: precedence and associativity but however I think that
the
comma operator when we overload it don't respect the PDO rules where
is guaranted the left-to-right evaluation (as && || and ?:)

Precedence and associativity don't define order of operations.
Overloading operators changes neither of these.

Overloading operators ||, &&, and , does alter their LEFT
SIDE FIRST rule as well as in the case of the two formers
the REQUIRED omission of the evaluation of the second
operand if the first is true (for or) or false (for and).

Mar 15 '07 #30

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

Similar topics

17
4727
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP manual mentions "overloading" (http://no.php.net/manual/en/language.oop5.overloading.php), but it isn't really overloading at all... Not in the sense it's used in other languages supporting overloading (such as C++ and Java). As one of the...
34
6472
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment operator. Here's what I'm trying to do. I've defined class Complex as class Complex { friend ostream &operator<<( ostream &, Complex & ); public: Complex( float = 0.0, float = 0.0 );
39
2192
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any C implementation supporting this feature? I assume some of you will claim that there is no need in function overloading, so I would like to know your arguments too. Thanks,
31
2294
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
10
2745
by: Pantokrator | last post by:
Hi, Is there any way to "overload" a struct? e.g. having already struct stA1 { int i_ID; int i_Type; };
2
4443
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream & operator>(std::istream & in, const Complex & a); // the methods correspond to the friend std::istream & operator>(std::istream & in, const Complex & a) { std::cout << "real: ";
0
9519
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
10214
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...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
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
9042
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...
1
7540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
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...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
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.