[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: i = i++



i think you are commiting a gross blunder.

watch this:
> A operator ++(int)
> {A temp(num) ; num = num + 1 ; return temp ;}

by A temp(num) you make a new object of type A with the value 1; then you
increment the value of the member variable num of the current instance which
becomes 2; finally use return temp, what implicitly follows is that a
bitwise member-to-member copy of temp is made into a. since the member
variable num of temp was never incremented your value stays at 1. do the
following changes and it works

> operator ++(int)
> {num = num + 1 ;}

hope it helps
bye
rahul

----- Original Message -----
From: "sindhoor pangal" <sindhoor@xxxxxxxxx>
To: "LIP" <linux-india-programmers@xxxxxxxxxxxxxxxxxxxxx>
Sent: Wednesday, January 10, 2001 6:53 PM
Subject: [LIP] i = i++


> hi
> what should the outcome of this program be ?
>
> void main()
> {
>     int i = 1 ;
>     i = i++ ;
>     cout<<i ;
> }
>
> i got the oputput as 2. but the following program gave
> me 1
>
> class A
> {
>     int num ;
>     public :
> A(int n) : num(n) {}
> A & operator = (const A & rhs)
> {num = rhs.num ; return *this ;}
> A operator ++(int)
> {A temp(num) ; num = num + 1 ; return temp ;}
> operator int()
> {return num ;}
> } ;
>
> void main()
> {
>     A a(1) ;
>     a = a++ ;
>     cout<<a ;
> }
>
> the result is the same on g++ and vc. i dont
> understand. please help.
> sindhoor
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Photos - Share your holiday photos online!
> http://photos.yahoo.com/
>
> ---------------------------------------------
> Find out more about this and other Linux India
> mailing lists at http://lists.linux-india.org/
>