* c++ post and pre increment operators - slightly off topic
@ 2000-07-25 17:57 Gianni Mariani
2000-07-26 5:25 ` llewelly
2000-07-26 16:59 ` Lei Ming
0 siblings, 2 replies; 4+ messages in thread
From: Gianni Mariani @ 2000-07-25 17:57 UTC (permalink / raw)
To: gcc-help
Please excuse my digression, it seems I have run out of other
references.
It seems c++ post increment methods don't happen post the expression.
A class that implements operator++() operator++(int) and operator*()
does not behave like (I at least) expected.
int i = * pointer_val ++;
Seems to behave differently if pointer_val is a real pointer. The post
increment operator happens BEFORE the reference.
Am I missing somthing. Same thing happens on windoze.
------- output of the code below --------
perator++(int) called
operator*() called
operator++() called
operator*() called
x1=1 x2=1 <<< --
x1=0 x2=1 <<< -- expected these 2 lines to be the same.
------- the code below ----------
#include <iostream.h>
class foo {
public:
int i;
foo( int x = 0 )
: i( x )
{
}
int operator*()
{
cout << "operator*() called\n";
return i;
}
foo operator++()
{
cout << "operator++() called\n";
++ i;
return * this;
}
foo operator++(int num)
{
cout << "operator++(int) called\n";
i ++;
return * this;
}
};
int intbar[] = { 0, 1, 2 };
main()
{
foo foobar1;
foo foobar2;
int x1 = *foobar1++;
int x2 = *++foobar2;
cout << "x1=" << x1 << " x2=" << x2 << endl;
int * intbar1 = intbar;
int * intbar2 = intbar;
x1 = *intbar1++;
x2 = *++intbar2;
cout << "x1=" << x1 << " x2=" << x2 << endl;
}
[gianni@uluru ds]$ ls
Debug/ ccstring.cpp ccstring.h jjnk.cpp* jjnk.dsp* jjnk.ncb*
jjnk.plg*
[gianni@uluru ds]$ cat jjnk.cpp
#include <iostream.h>
class foo {
public:
int i;
foo( int x = 0 )
: i( x )
{
}
int operator*()
{
cout << "operator*() called\n";
return i;
}
foo operator++()
{
cout << "operator++() called\n";
++ i;
return * this;
}
foo operator++(int num)
{
cout << "operator++(int) called\n";
i ++;
return * this;
}
};
int intbar[] = { 0, 1, 2 };
main()
{
foo foobar1;
foo foobar2;
int x1 = *foobar1++;
int x2 = *++foobar2;
cout << "x1=" << x1 << " x2=" << x2 << endl;
int * intbar1 = intbar;
int * intbar2 = intbar;
x1 = *intbar1++;
x2 = *++intbar2;
cout << "x1=" << x1 << " x2=" << x2 << endl;
}
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: c++ post and pre increment operators - slightly off topic
2000-07-25 17:57 c++ post and pre increment operators - slightly off topic Gianni Mariani
@ 2000-07-26 5:25 ` llewelly
2000-07-26 16:59 ` Lei Ming
1 sibling, 0 replies; 4+ messages in thread
From: llewelly @ 2000-07-26 5:25 UTC (permalink / raw)
To: Gianni Mariani; +Cc: gcc-help
On Tue, 25 Jul 2000, Gianni Mariani wrote:
>
> Please excuse my digression, it seems I have run out of other
> references.
In the future, please post C++ questions to comp.lang.c++.moderated; you
will usually get more and better answers.
>
> It seems c++ post increment methods don't happen post the expression.
>
> A class that implements operator++() operator++(int) and operator*()
> does not behave like (I at least) expected.
>
> int i = * pointer_val ++;
>
> Seems to behave differently if pointer_val is a real pointer. The post
> increment operator happens BEFORE the reference.
>
> Am I missing somthing. Same thing happens on windoze.
>
> ------- output of the code below --------
>
> perator++(int) called
> operator*() called
> operator++() called
> operator*() called
> x1=1 x2=1 <<< --
> x1=0 x2=1 <<< -- expected these 2 lines to be the same.
>
> ------- the code below ----------
>
>
> #include <iostream.h>
>
> class foo {
> public:
> int i;
>
> foo( int x = 0 )
> : i( x )
> {
> }
>
> int operator*()
> {
> cout << "operator*() called\n";
> return i;
> }
>
> foo operator++()
> {
> cout << "operator++() called\n";
> ++ i;
> return * this;
> }
>
> foo operator++(int num)
> {
> cout << "operator++(int) called\n";
> i ++;
> return * this;
> }
foo::operator++(int) is supposed to make a copy of *this, increment *this
(leaving the copy unchanged), and return the copy:
const foo operator++(int)
{
foo ret(*this);
operator++();
return ret;
}
should work the way you expect.
>
> };
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: c++ post and pre increment operators - slightly off topic
2000-07-25 17:57 c++ post and pre increment operators - slightly off topic Gianni Mariani
2000-07-26 5:25 ` llewelly
@ 2000-07-26 16:59 ` Lei Ming
2000-07-27 11:12 ` Gianni Mariani
1 sibling, 1 reply; 4+ messages in thread
From: Lei Ming @ 2000-07-26 16:59 UTC (permalink / raw)
To: marianig, gcc-help
Hi,
You post your question to wrong place. You should go to c++ news group.
Post increment did go before reference. The problem is your
implementation.
In "foo operator ++(int num)", you should return the old value not the new
value. That is the semantic meaning of post increment.
Lei
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: c++ post and pre increment operators - slightly off topic
2000-07-26 16:59 ` Lei Ming
@ 2000-07-27 11:12 ` Gianni Mariani
0 siblings, 0 replies; 4+ messages in thread
From: Gianni Mariani @ 2000-07-27 11:12 UTC (permalink / raw)
To: Lei Ming; +Cc: gcc-help
Excellent idea - tried it - no luck - article -
97DF3CB.F0B9F9AB%40orconet.com .
I thought that the semanic was that the operator was a
"side effect", hence the method should be called after the
expression was evaluated. My bad. Hence I thought
it was a compiler boog.
my *bad* assumption was that :
y = z ++ + z ++ should be the same as (y = z + z), z++, z++
I could not find any reference to the contrary. I did RTFM.
Lei Ming wrote:
> Hi,
>
> You post your question to wrong place. You should go to c++ news group.
>
> Post increment did go before reference. The problem is your
> implementation.
> In "foo operator ++(int num)", you should return the old value not the new
>
> value. That is the semantic meaning of post increment.
>
> Lei
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2000-07-27 11:12 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-07-25 17:57 c++ post and pre increment operators - slightly off topic Gianni Mariani
2000-07-26 5:25 ` llewelly
2000-07-26 16:59 ` Lei Ming
2000-07-27 11:12 ` Gianni Mariani
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).