How to cast/convert pointer to reference in C++

C++PointersCastingReferenceType Conversion

C++ Problem Overview


How can I pass a pointer (Object *ob) to a function which prototype is void foo(Object &) ?

C++ Solutions


Solution 1 - C++

Call it like this:

foo(*ob);

Note that there is no casting going on here, as suggested in your question title. All we have done is de-referenced the pointer to the object which we then pass to the function.

Solution 2 - C++

foo(*ob);

You don't need to cast it because it's the same Object type, you just need to dereference it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDewsworldView Question on Stackoverflow
Solution 1 - C++David HeffernanView Answer on Stackoverflow
Solution 2 - C++Roee GavirelView Answer on Stackoverflow