[Discuss] OT - C++ operator overloading question.

Adam Parkin pzelnip at telus.net
Wed Aug 30 08:20:51 PDT 2006


John Vetterli wrote:
>>   NumData* myNumData = new NumData(1, 10);
>>   myNumData->displayInfo();
>>   ++myNumData;
>>   myNumData->displayInfo();
> 
> Oops, you're incrementing the pointer, not the object it's pointing to. 
> Try ++*myNumData instead of ++myNumData.

Yup, that's right.  Or you could do one better: use a reference instead 
of a pointer:

NumData & myNumData = new NumData(1, 10);
myNumData.displayInfo();
++myNumData;
myNumData.displayInfo();

> 
> Also, IIRC, operator++() and operator--() define postincrement and 
> postdecrement operators; use operator++(int) and operator--(int) to 
> define preincrement and predecrement (the int is just a dummy value).

Anybody ever come up with a good way to remember which is the 
preincrement prototype and which is the postincrement?  I have to look 
it up every time I write code that overloads these operators.
-- 
--
Adam Parkin
E-mail: pzelnip at telus.net
----------------------
  The point of philosophy is to start with something so simple as to 
seem not worth stating, and to end with something so paradoxical that no 
one will believe it.
	-- Bertrand Russell, Science and Religion


More information about the Discuss mailing list