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

David Bronaugh dbronaugh at linuxboxen.org
Wed Aug 30 14:02:19 PDT 2006


Adam Parkin wrote:
> 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();
Or... you could simply do this:
NumData myNumData(1, 10);

.. and let the rest follow. AFAIK this is valid syntax; it'll allocate 
myNumData on the stack.

While not the greatest idea for huge data structures, for something 
small and lightweight like this, it is ideal.

Plus you don't have to think about memory management; when it goes out 
of scope, you're done.

David


More information about the Discuss mailing list