operator->() overload not being called 
Author Message
 operator->() overload not being called

Hi.

I defined an overloaded member-access operator-> but it's not being
called...why?
Here's a sample

class CClass
{
public:
   CClass() {}
   ~CClass() {}

   CClass* operator->() {  return this; }

   void DoFoo( ) { return; }

Quote:
}

Now what happens when I call the class on a pointer to it in my client app
is that DoFoo( ) gets called directly.
I never hit the overloaded operator.

CClass* pClass = new CClass;

pClass->DoFoo();

Shouldn't the operator-> overload be called first, then DoFoo()??

Thanks,

~colinrei



Mon, 04 Aug 2003 11:28:57 GMT  
 operator->() overload not being called


Quote:

> Now what happens when I call the class on a pointer to it in my client app
> is that DoFoo( ) gets called directly.
> I never hit the overloaded operator.

> CClass* pClass = new CClass;

> pClass->DoFoo();

> Shouldn't the operator-> overload be called first, then DoFoo()??

Operator overloads operate on objects not on pointers to objects.  If you
were to write "(*pClass)->DoFoo();" you would see your operator called.  C++
allows overloading operator -> to allow a class to behave as a pointer,
operator overloads do not change the behavior of those operators when
applied to pointers.


Mon, 04 Aug 2003 11:33:34 GMT  
 operator->() overload not being called

Quote:

> Hi.

> I defined an overloaded member-access operator-> but it's not being
> called...why?

Because you have to call it on an object, not a pointer.

Consider the following program:

int main()
{
 CClass a;
 a->DoFoo();

Quote:
}

Of course, using -> in this context can be quite misleading but in real
world code, e.g. custom iterator objects, this is a common technique.

Danny Kalev

"The ANSI/ISO C++ Professional Programmer's Handbook"
http://www.amazon.com/exec/obidos/ASIN/0789720221
http://www.devx.com/free/books/bookreview.asp?bookid=286

Quote:
> Here's a sample

> class CClass
> {
> public:
>    CClass() {}
>    ~CClass() {}

>    CClass* operator->() {  return this; }

>    void DoFoo( ) { return; }
> }

> Now what happens when I call the class on a pointer to it in my client app
> is that DoFoo( ) gets called directly.
> I never hit the overloaded operator.

> CClass* pClass = new CClass;

> pClass->DoFoo();

> Shouldn't the operator-> overload be called first, then DoFoo()??

> Thanks,

> ~colinrei



Thu, 07 Aug 2003 11:38:08 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. overloading fstream operator>>

2. trunc <> floor (was: overloading operators)

3. Need templatized prototype for operator ->* overload

4. trouble overloading member selection (->) operator

5. Calling overloaded operator from ASP

6. displaying calls to overloaded operators

7. CXX0058: Error: Overloaded Operator Not Found

8. update overloading [][] ..working but not with a template operator ..arrrrrrrrrgghghhh

9. VC++ 5.0 ambiguity: conversion operator vs overloaded operator?

10. == and != operator (operator overloading)

11. overloaded virtual function not called

12. Overloaded CObject::Dump not called at program exit

 

 
Powered by phpBB® Forum Software