[Discuss] OT - C++ operator overloading question.
David Wu
david.yk.wu at gmail.com
Wed Aug 30 20:01:12 PDT 2006
i understand the idea of pre and post increments. As i understands is that
the parameter in the post-inc operator is simply used to differentiate from
the pre-inc operator, but its more like a coding practice then syntax
requirement. As i tested it out, if i do
(*myNumData)++; instead of the original, ++(*myNumData);
Then the compiler would compiles fine, but at runtime, I would get similiar
error as before,
Integer Value is 1
Long long Value is 10
Integer Value is 135153
Long long Value is 0
*** glibc detected *** free(): invalid pointer: 0x09559014 ***
Aborted
when I apply the ++ after the (*myNumData), the data become the lvalue of
the operation, but the compiler does not complain, so the " NumData&
operator++();" declarartion des not take the lvalue or rvalue into
accounts??
Thanks again.
DW
On 8/31/06, David Wu <david.yk.wu at gmail.com> wrote:
>
> Thanks guys,
>
> Ok I see the msitake now. Thanks lots!
>
>
> :)
>
> David
>
>
> On 8/30/06, John Vetterli <jvetterli at linux.ca> wrote:
> >
> > On Wed, 30 Aug 2006, David Wu wrote:
> > > ...
> > > class NumData
> > > {
> > > public:
> > > NumData(int a, long long b);
> > > ~NumData();
> > > NumData& operator++();
> > > NumData& operator--();
> > > void displayInfo();
> > > private:
> > > int m_nValue;
> > > long long m_llnValue;
> > > };
> > > ...
> > > int main()
> > > {
> > > NumData* myNumData = new NumData(1, 10);
> > > myNumData->displayInfo();
> > > ++myNumData;
> > > myNumData->displayInfo();
> > > if(myNumData != NULL)
> > > {
> > > delete myNumData;
> > > myNumData = NULL;
> > > }
> > > return 0;
> > > }
> > > ===============================================
> >
> > Oops, you're incrementing the pointer, not the object it's pointing to.
> > Try ++*myNumData instead of ++myNumData.
> >
> > 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).
> >
> > HTH
> > JV
> > _______________________________________________
> > Discuss mailing list
> > Discuss at vlug.org
> > http://ladybug.vlug.org/cgi-bin/mailman/listinfo/discuss
> >
>
>
More information about the Discuss
mailing list