Dewsworld
Dewsworld

Reputation: 14033

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

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

Upvotes: 212

Views: 203470

Answers (2)

Roee Gavirel
Roee Gavirel

Reputation: 19445

foo(*ob);

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

Upvotes: 63

David Heffernan
David Heffernan

Reputation: 612784

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.

Upvotes: 304

Related Questions