Connecting Tech Pros Worldwide Forums | Help | Site Map

Hint of operator overloading

dover
Guest
 
Posts: n/a
#1: Jul 22 '05
Could somebody give hints about writing overloading of post-increment and
pre-increment operators? Thanks!




Nolan Martin
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Hint of operator overloading



"dover" <doverkn@close.com> wrote in message
news:p82Mc.125754$OB3.47817@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
> Could somebody give hints about writing overloading of post-increment and
> pre-increment operators? Thanks!
>
>
>[/color]

Try
this...http://jamesthornton.com/eckel/TICPP...Chapter12.html


Dave Townsend
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Hint of operator overloading


Here, try this. The trick is the dummy parameter (int) in the pre-increment
operator, which is used to disambiguate the ++foo and foo++ operators.

dave

class foo
{

public:
foo( int i )
:_i(i)
{}
foo()
:_i(0)
{}
// pre-increment ++foo
foo operator++()
{
_i++;
return *this;
}
// post increment foo++
foo operator++(int )
{
foo tmp = *this;
_i++;
return tmp;

}
int toInt()
{
return _i;
}
private:
int _i;

};



#include "stdafx.h"

int main(int argc, char* argv[])
{

foo f(10);
foo g(10);
foo gnew(++g);
foo fnew(f++);

int gi = g.toInt();
int fi = f.toInt();

int gnewi = gnew.toInt();
int fnewi = fnew.toInt();


return 0;
}


"dover" <doverkn@close.com> wrote in message
news:p82Mc.125754$OB3.47817@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
> Could somebody give hints about writing overloading of post-increment and
> pre-increment operators? Thanks!
>
>
>[/color]


Closed Thread