473,795 Members | 2,512 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
Precedence and associativity don't define order of operations.
Overloading operators changes neither of these.
No? and how the compiler could know which operations came fisrt????
it is the same algebra principles!
May be I don't have understand what you want to say?

Best
Mar 15 '07 #31
josh wrote:
>Precedence and associativity don't define order of operations.
Overloading operators changes neither of these.

No? and how the compiler could know which operations came fisrt????
it is the same algebra principles!
May be I don't have understand what you want to say?
My guess [as to what ?? wants to say] follows.

In an expression a*b + c*d both multiplications have to be done
before the sum can be calculated, but it is unspecified in the
language whether 'a' is multiplied by 'b' first and then 'c' by
'd' or vice versa. Precedence only says that the expression
a*b + c*d cannot be interpreted as a * (b+c) * d.

Same with overloaded operators: the expression a*b + c*d can be
rewritten as operator+(opera tor*(a,b), operator*(c,d)) and the
order in which the arguments of the operator+ function (the return
values of the two calls to operator*) are calculated is not set by
the standard. The generated code can call the first operator*
before the second or vice versa.

Precedence cannot be changed for overloaded ops, that is, you
cannot expect the expression a*b + c*d to evaluate as
operator*(a, operator+(b, operator*(c,d)) ).

[BTW, please give proper attributions when you quote. It is not
clear to whom you replied and whom you meant when you wrote in
your reply "... what *you* want to say", emphasis mine]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 15 '07 #32
In an expression a*b + c*d both multiplications have to be done
before the sum can be calculated, but it is unspecified in the
language whether 'a' is multiplied by 'b' first and then 'c' by
'd' or vice versa. Precedence only says that the expression
a*b + c*d cannot be interpreted as a * (b+c) * d.
I think your guess is ok...


Mar 15 '07 #33
On 03/14/07, "josh" <xd********@yah oo.comsaid:
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
Not necessarily. There is nothing stopping the compiler from evaluating
c first, *then* evaluating operator&&(a,b) . The only guarantee that we
have as to the ordering of the evaluations of a, b and c is that c
cannot come between a and b. These are the possible orderings:

a, b, c
b, a, c
(b and a simultaneously) , c
c, a, b
c, b, a
c, (b and a simultaneously)
--
Clark S. Cox III
cl*******@gmail .com

Mar 15 '07 #34
josh wrote:
>Precedence and associativity don't define order of operations.
Overloading operators changes neither of these.

No? and how the compiler could know which operations came fisrt????
it is the same algebra principles!
May be I don't have understand what you want to say?
Precedence and associativity affect the meaning of the operators as
to which operator binds to which operands.

The ordering however in C++ is UNSPECIFIED other than obviously the
values that are needed for later stages in the calculation have to
be done earlier. However, the ones that don't matter can be done
in any order. Side-effects can be applied at any time up until
the sequence point.

a() * b() * c()

for example could execute the functions a, b, and c before
it does nay of the multiples, or it could do them immediately
before the value is needed...
Mar 16 '07 #35
On Mar 16, 4:48 am, Clark Cox <clarkc...@gmai l.comwrote:
operator&&(oper ator&&(a,b),c);

There is nothing stopping the compiler from evaluating
c first, *then* evaluating operator&&(a,b) . The only guarantee that we
have as to the ordering of the evaluations of a, b and c is that c
cannot come between a and b.
There is no such guarantee. For example, the order could be a,c,b.

Mar 16 '07 #36

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...
0
10438
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
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...
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...
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...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.