Tony The Lion
Tony The Lion

Reputation: 63310

passing pointers to function that takes a reference?

In C++, when you have a function that takes a reference to an object, how can you pass an object pointer to it?

As so:

Myobject * obj = new Myobject();

somefunc(obj);  //-> Does not work?? Illegal cast??

somefunc(Myobject& b)
{
 // Do something
}

Upvotes: 24

Views: 9500

Answers (2)

Guillaume Lebourgeois
Guillaume Lebourgeois

Reputation: 3873

you just have to do :

somfunc(*obj);

Upvotes: 5

GManNickG
GManNickG

Reputation: 504293

Just dereference the pointer, resulting in the lvalue:

somefun(*obj);

Upvotes: 30

Related Questions