+ 2
Operator -> for shared pointer
Hi I have a member variable T* m_ptr into templated class. This is in line with shared pointer implementation. My query is operator overloaded function as below: T* operator->() { return m_ptr; } Why this pointer directly call method ? Should it not be mysharedptr->->mymethod() Double time -> is not needed?
3 Respuestas
+ 1
Thanks
+ 1
-> was not available to use before you overloaded it. How can there be the first -> in '->->' ? If you can already use ->, why is there the need to define an operator overlaod in the first place?
0
T* operator->() const is the concern here for me.
This is called from last line of the main function.
bptr->display() results into printing display by calling a function display.
Now comes tricky part.
bptr is object of class custom shared pointer. Overloaded -> operator I.e. bptr-> will gives us T* from class member. This T* should need now another -> to call display method.
In other word , Should it not be (bptr->)->display()?
https://sololearn.com/compiler-playground/chbZHAzzB0Rz/?ref=app