Or if i need to do that, then i should just use shared_ptr?
I know i have many questions regarding smart pointer - i still don't quite understand when to use which smart pointer.
Or if i need to do that, then i should just use shared_ptr?
I know i have many questions regarding smart pointer - i still don't quite understand when to use which smart pointer.
Yes you can pass it by reference.
However, if the function just wants to use the managed object, you might consider passing a reference to the object itself.
void foo(const boost::scoped_ptr<Object>& o)
{
o->foobar();
}
void bar(const Object& o)
{
o.foobar();
}
The difference is that in the first case you have coupled the function with a particular smart pointer type.
Object o;
boost::scoped_ptr<Object> scoped(new Object);
boost::shared_ptr<Object> shared(new Object);
foo(o); //no
foo(scoped); //OK
foo(shared); //no
bar(o); //OK
bar(*scoped); //OK
bar(*shared); //OK
Generally I'd only pass the scoped_ptr, if the intention is to do something with the scoped_ptr instance itself (e.g release or reset the resource). Similarly for shared_ptr (e.g the function wants to share the resource with other shared pointers).