Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Hint of operator overloading

Reply
Thread Tools

Hint of operator overloading

 
 
dover
Guest
Posts: n/a
 
      07-23-2004
Could somebody give hints about writing overloading of post-increment and
pre-increment operators? Thanks!



 
Reply With Quote
 
 
 
 
Nolan Martin
Guest
Posts: n/a
 
      07-23-2004

"dover" <(E-Mail Removed)> wrote in message
news82Mc.125754$(E-Mail Removed)...
> Could somebody give hints about writing overloading of post-increment and
> pre-increment operators? Thanks!
>
>
>


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


 
Reply With Quote
 
 
 
 
Dave Townsend
Guest
Posts: n/a
 
      07-23-2004
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" <(E-Mail Removed)> wrote in message
news82Mc.125754$(E-Mail Removed)...
> Could somebody give hints about writing overloading of post-increment and
> pre-increment operators? Thanks!
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading operator->*() and operator->() gob00st@googlemail.com C++ 2 02-21-2009 04:26 AM
overloading operator->*() and operator->() gob00st@googlemail.com C++ 11 02-20-2009 08:52 PM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
Why is overloading operator. (member operator) forbidden? dascandy@gmail.com C++ 11 05-16-2007 07:54 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM



Advertisments