0

While overloading operator* we do as follows:

T & operator * () {  return *ptr; }

That means if I have:

SmartPtr<int> obj(new int());
*Obj = 20;
Cout << *obj;

Then the *obj part is replaced by *ptr so its as good as:

*Ptr=20
cout<<*ptr

Now while overloading operator-> we do the following:

T * operator -> () { return ptr; }

And it can be accessed as:

Obj->myfun();

What I don't understand here is after evaluating the obj-> is replaced by ptr so it should look as follows:

ptrmyfun(); // and this is syntax error

Where am I going wrong?

anurag86
  • 1,635
  • 1
  • 16
  • 31
  • no, according to your logic * operator should also be replaced like *ptrobj, but that's not how it works, check this answer as it explains those tricky operators clearly http://stackoverflow.com/a/8782794/1936366 – Onur A. Feb 12 '17 at 03:03
  • C++ does not work like search/replace in a text editor. – Sam Varshavchik Feb 12 '17 at 03:04
  • @Onur A. Why should * operator replace it with *ptrobj? It clearly should not. This problem would occur only in case of -> – anurag86 Feb 12 '17 at 03:05
  • I repeat: C++ does not work like search/replace in a text editor. "obj1->" is **not** replaced by `ptr` verbatim, as if this was a line of text in a text editor. The `->` operator results in the custom `operator->`() getting invoked. The return value from the operator function becomes the actual pointer to which the actual pointer dereference gets applied to. The End. – Sam Varshavchik Feb 12 '17 at 03:09
  • The `->` operator automatically chains the `->` of the result. It can be another user-defined `->`, but this chaining continues until an actual pointer is returned. So `Obj->myfun()` might be thought of as `(Obj->)->myfun()`. – Christopher Oicles Feb 12 '17 at 03:11
  • @Sam, christopher : Yes that is it. Why dont one you guys Post this as an answer so that the same can be be accepted – anurag86 Feb 12 '17 at 03:14
  • Go ahead @Sam Varshavchik – Christopher Oicles Feb 12 '17 at 03:25
  • @anurag86 according to your logic in the question, that leads to *ptrobj, You said in your question Obj->myfun(); translates to ptrmyfun(); so applying your logic *Obj = 20; translates to *ptrObj = 20; but I reiterate that's not how it works. – Onur A. Feb 12 '17 at 12:23

1 Answers1

0

C++ does not work like search/replace in a text editor. "obj1->" is not replaced by ptr verbatim, as if this was a line of text in a text file.

With:

Obj->myfun();

The fact that the -> operator invokes a custom operator that returns ptr:

T * operator -> () { return ptr; }

That doesn't mean that the original statement somehow becomes

ptrmyfun();

As if "Obj->" was replaced by "ptr". C++ simply does not work this way, like search/replace in a text editor.

The -> operator results in the custom operator->() getting invoked. That part, of how you understand operator overloading works, is correct.

But what happens is that, simply, the return value from the operator->() becomes the actual pointer value to which the actual pointer dereference gets applied to.

So this becomes, essentially:

ptr->myfun();

That's all.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148