0

operator overloading for ->

// why -> operator is not called from tp->display(); function call ? https://code.sololearn.com/cQgeRCSXK20c/?ref=app I know my function overloaded are not solving actual purpose of operator but I am curious why -> is not calling overloaded operator ??

22nd Jan 2021, 7:23 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
6 ответов
+ 1
Ketan Lalcheta I guess you've ever heard of smart pointers, right? They are not pointers, they are objects that tend to behave like pointers due to the overloading of the arrow operator!
23rd Jan 2021, 3:21 PM
Anthony Maina
Anthony Maina - avatar
+ 1
Simply because you're using a pointer and not a Test object to access the display member function. If you wanted the operator->() function to be called, then use the either the object t1 or t2. e.g. t1->display(); Note however that for this to work the overloaded operator-> function is supposed to return the this pointer since that is what will be used to access the member function display. However, if you still want the overloaded function to return void, then you'll have to call it explicitly. e.g t1.operator->(); or using tp tp->operator->();
22nd Jan 2021, 8:45 PM
Anthony Maina
Anthony Maina - avatar
+ 1
Yeah got it...thanks
23rd Jan 2021, 3:22 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Thanks Anthony Maina I changed the operator-> function now.. it returns Test & instead of void Also I don't want to call arrow operator function on object... I just want to overide behaviour when pointer access the member function using arrow operator... So I am confused how to call operator arrow overloaded by default when I call pointer->member function ();
23rd Jan 2021, 7:02 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Ketan Lalcheta It's not possible to override the default behaviour when using pointers to access member functions using the arrow operator. That only works for objects
23rd Jan 2021, 7:42 AM
Anthony Maina
Anthony Maina - avatar
0
So isn't it funny to have overloaded operator for -> No one would call this operator with object as main use of arrow operator is while we deal with pointer If it is not allowed , then what benifit we get when we overload arrow operator ? If no benifit is there , should not it be not allowed to be overloaded like scope resolution and few other operators ?
23rd Jan 2021, 3:18 PM
Ketan Lalcheta
Ketan Lalcheta - avatar