[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]
RE: i = i++
Hi,
In the case of C++ you are determinign in the postincrement
temp=num;
num=num+1;
return temp;
when u return this we will have old value in temp we will have and
incremented value in num=2;
after that we are doing assignment num=temp(uisng operator =) now num
contains 1.
where as in the case of c it just an increment(Mind the result depending the
compiler in this case).
This is indicating that by using operator overloading we can solve this kind
of problems.
Thanks,
Kondaiah Dasari
> -----Original Message-----
> From: sindhoor pangal [SMTP:sindhoor@xxxxxxxxx]
> Sent: Wednesday, January 10, 2001 6:53 PM
> To: LIP
> 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/