Pass base pointer reference | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Pass base pointer reference

Please refer code below: https://code.sololearn.com/cl48KxgAsSq1 Can we achieve the update method which takes base pointer as reference? Doing so works fine in case of Base* pb , but getting compile time error for scenario of Derived* pb. How to achieve this?

5th Jul 2022, 8:52 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Answers
+ 4
This is because your Derived* pointer gets casted to a Base* on line 48. Since the casted pointer is an rvalue, you can't pass it to a function that takes lvalue reference. Assigning the Derived* pointer to a Base* pointer first and then passing that will work https://code.sololearn.com/cEpYOhei3A1W/?ref=app
5th Jul 2022, 12:48 PM
XXX
XXX - avatar
0
I ran it just now and got no error. If I understood the code correctly, everything worked fine. Could you pls elaborate on the issue?
5th Jul 2022, 11:15 AM
Emerson Prado
Emerson Prado - avatar
0
Change update function signature as below and you will get an error: void Update(Base*& pb)
5th Jul 2022, 11:45 AM
Ketan Lalcheta
Ketan Lalcheta - avatar