473,795 Members | 2,892 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
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 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.
Mar 13 '07 #11
On 12 Mar, 13:16, Michael DOUBEZ <michael.dou... @free.frwrote:
josh a écrit :
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);

Because you have 5.
The expression evaluates:
Other ox = ( (o1 + o1) , (o3 = o2 + o1) );

Or with names
Other ox = o1.operator+(o1 ).operator,(o3. operator=(o2.op erator+(o1)));

Since o3 is 5, then o1 is also 5 and ox is 5.

The reason is overloaded operator, doesn't have the same precedence as
POD operator,. Is is very confusing.
// ------------------
cout << ox.i << endl;
return 0;
}

Michael
so the compiler is doing:
o1.operator+(01 ).operator,(03 = 02.operator+(01 ))
and evaluating it to 5
but when we define overloaded operators the rules "should" be that
they have the same precedence and the same associativity and the same
arity of
the PDO and so really I don't understand why here the rules seems to
be "could"....
Here in the code it seems that it doesn't save the first evaluating
operation on o1...
may be only for the comma operators is there an exception rule????

Mar 13 '07 #12
On 12 Mar, 11:17, "josh" <xdevel2...@yah oo.comwrote:
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;

}
the precedence, associativity and arity of overload operator, as rule,
"should" be the same for the PDO but here for the comma it doesn't
seem so...
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
o1 + (the mul result).
So I think that the compiler when meet that expression will do:
o1.operator+(o2 .operator*(o3))
but it should do the same with:
(o1.operator+(o 1)).operator,(o 3 = 02.operator+(o1 ))
so it should evaluate from left to right as the same PDO rule...
so what's wrong?

Mar 13 '07 #13
On 12 Mar, 13:16, Michael DOUBEZ <michael.dou... @free.frwrote:
josh a écrit :
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);

Because you have 5.
The expression evaluates:
Other ox = ( (o1 + o1) , (o3 = o2 + o1) );

Or with names
Other ox = o1.operator+(o1 ).operator,(o3. operator=(o2.op erator+(o1)));

Since o3 is 5, then o1 is also 5 and ox is 5.

The reason is overloaded operator, doesn't have the same precedence as
POD operator,. Is is very confusing.
// ------------------
cout << ox.i << endl;
return 0;
}

Michael
the precedence, associativity and arity of overload operator, as rule,
"should" be the same for the PDO but here for the comma it doesn't
seem so...
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
o1 + (the mul result).
So I think that the compiler when meet that expression will do:
o1.operator+(o2 .operator*(o3))
but it should do the same with:
(o1.operator+(o 1)).operator,(o 3 = 02.operator+(o1 ))
so it should evaluate from left to right as the same PDO rule...
so what's wrong?

Mar 13 '07 #14
josh wrote:
On 12 Mar, 11:17, "josh" <xdevel2...@yah oo.comwrote:
>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;

}

the precedence, associativity and arity of overload operator, as rule,
"should" be the same for the PDO but here for the comma it doesn't
seem so...
Precedence and arity do match. I have no idea, what you mean by
associativity. Anyhow, what an overloaded comma operator lacks is the
sequence point that the built-in comma operator gives you.

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].

o1 + (the mul result).
So I think that the compiler when meet that expression will do:
o1.operator+(o2 .operator*(o3))
but it should do the same with:
(o1.operator+(o 1)).operator,(o 3 = 02.operator+(o1 ))
so it should evaluate from left to right as the same PDO rule...
so what's wrong?
Your code has undefined behavior since it modifies the same object several
times without sequence points. The overloaded comma operator does not give
you the sequence point that forces left-to right evaluation.
Best

Kai-Uwe Bux
Mar 13 '07 #15
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 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.?

Mar 13 '07 #16
BTW - What is POD?

Mar 13 '07 #17
liam_herron wrote:
BTW - What is POD?
Plain Old Data.

Mar 13 '07 #18
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 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.
Mar 14 '07 #19
Precedence and arity do match. I have no idea, what you mean by
associativity.
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

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.
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

Mar 14 '07 #20

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
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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
6780
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.