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;
} 35 2071
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
josh wrote:
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;
}
I suspect that this could be undefined behaviour as the + operators have
equal precedence and the order they are evaluated depends on the
compiler. evaluation order is '+', '=', ',' based on precedence and
hence the code is not safe.
JB
Michael DOUBEZ wrote:
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.
That is not the only thing confusing! Your use of operator,() is the so
obfuscated! Why on Earth would you want to do something like that?
What possible reason could you have?
On a side note, why isn't operator,() the same precedence as the regular
',' operator? I thought that all user defined operators had the same
precedence as the builtins.
Adrian
--
=============== =============== =============== =============
Adrian Hawryluk BSc. Computer Science
----------------------------------------------------------
Specialising in: OOD Methodologies in UML
OOP Methodologies in C, C++ and more
RT Embedded Programming
__--------------------------------------------------__
----- [blog: http://adrians-musings.blogspot.com/] -----
'--------------------------------------------------------'
My newsgroup writings are licensed under the Creative
Commons Attribution-Noncommercial-Share Alike 3.0 License http://creativecommons.org/licenses/by-nc-sa/3.0/
=============== =============== =============== =============
josh wrote:
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;
}
You are modifying and evaluating the same object (o1) between sequence
points. Your program's behavior is therefore undefined.
Please see the FAQ on ++i,i++, and following (39.15 and 39.16) http://www.parashift.com/c++-faq-lit...html#faq-39.15
Adrian Hawryluk a écrit :
Michael DOUBEZ wrote:
>josh a écrit :
>>Hi, I coded the following but It does not return what I expect, why? [snip] // 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.
That is not the only thing confusing! Your use of operator,() is the so
obfuscated! Why on Earth would you want to do something like that? What
possible reason could you have?
Mostly for learning what not to do I guess.
blitz++ has matrix initalisation with comma overloading.
>
On a side note, why isn't operator,() the same precedence as the regular
',' operator? I thought that all user defined operators had the same
precedence as the builtins.
Concerning the precedence, it is sloppy writing from my part: it doesn't
have the same ordering properties but the precedence is kept (the lowest
if I remember correctly). The point is: in POD, the comma operator is a
sequence point and evaluates from left to right, the lhs of comma being
discarded while when overloaded, it is just another function (no
sequence point).
(++i),(i*=2) is defined behavior with POD while with Operator,(): it is UB.
This was the case up in the OP.
Michael
red floyd wrote:
josh wrote:
>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; }
You are modifying and evaluating the same object (o1) between sequence
points. Your program's behavior is therefore undefined.
Please see the FAQ on ++i,i++, and following (39.15 and 39.16)
http://www.parashift.com/c++-faq-lit...html#faq-39.15
Further explanation:
Because operator+ modifies its object **AND** you've overloaded
operator, it's equivalent to calling operator,(x++,x ) -- undefined behavior
Adrian Hawryluk wrote:
>The reason is overloaded operator, doesn't have the same precedence as POD operator,. Is is very confusing.
That is not the only thing confusing! Your use of operator,() is the so
obfuscated! Why on Earth would you want to do something like that?
What possible reason could you have?
Actually, all the overloaded operators in the OP's code are doing confusing
things.
On Mar 13, 10:07 am, red floyd <no.s...@here.d udewrote:
// HERE it returns 5 AND not 6 WHY ??????????????? ????
Other ox = (o1 + o1, o3 = o2 + o1);
You are modifying and evaluating the same object (o1) between sequence
points. Your program's behavior is therefore undefined.
Not true, there is a sequence point before and after every
function call -- in particular, operator+ and operator,
Further explanation:
Because operator+ modifies its object **AND** you've overloaded
operator, it's equivalent to calling operator,(x++,x ) -- undefined behavior
Not equivalent at all. It's equivalent to h(x.f(x), g(x))
which is fine (even if f modifies x).
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.
Old Wolf wrote:
On Mar 13, 10:07 am, red floyd <no.s...@here.d udewrote:
>>> // HERE it returns 5 AND not 6 WHY ??????????????? ???? Other ox = (o1 + o1, o3 = o2 + o1); You are modifying and evaluating the same object (o1) between sequence points. Your program's behavior is therefore undefined.
Not true, there is a sequence point before and after every
function call -- in particular, operator+ and operator,
>Further explanation:
Because operator+ modifies its object **AND** you've overloaded operator, it's equivalent to calling operator,(x++,x ) -- undefined behavior
Not equivalent at all. It's equivalent to h(x.f(x), g(x))
which is fine (even if f modifies x).
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.
Adrian
--
=============== =============== =============== =============
Adrian Hawryluk BSc. Computer Science
----------------------------------------------------------
Specialising in: OOD Methodologies in UML
OOP Methodologies in C, C++ and more
RT Embedded Programming
__--------------------------------------------------__
----- [blog: http://adrians-musings.blogspot.com/] -----
'--------------------------------------------------------'
My newsgroup writings are licensed under the Creative
Commons Attribution-Noncommercial-Share Alike 3.0 License http://creativecommons.org/licenses/by-nc-sa/3.0/
=============== =============== =============== ============= This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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 );
|
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,
|
by: |
last post by:
Hi,
Why can I not overload on just the return type?
Say for example.
public int blah(int x)
{
}
|
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;
};
| |
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: ";
|
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...
|
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...
|
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,...
|
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...
|
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();...
| |
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| | |