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

Anthony Howe ahowe_ca at yahoo.ca
Wed Aug 30 09:17:44 PDT 2006


> > 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();

You probably don't want to use a reference instead of a pointer since there
is much more opportunity to leak memory.  If you were to use a reference you
would have to cast the new memory as well, and then remember to derefernce
the memory as well:

NumData & myNumData = *(new NumData(1, 10); myNumData.displayInfo());
. . .
delete &a;


Anthony



More information about the Discuss mailing list