Is this a proper way?
void helperFunc(MyClass *ptr)
{
// do something with ptr,
}
unique_ptr<MyClass> p(new MyClass());
helperFunc(p.get());
or should I use shared_ptr for such operations?
Unless you want to take ownership of the memory (in which case you should pass either a shared_ptr
or a unique_ptr
), why are you working with a pointer at all?
Pass by reference.
void helperFunc(MyClass& obj)
{
// do something with ptr,
}
unique_ptr<MyClass> p(new MyClass());
helperFunc(*p);
Using raw pointers when you don’t want to take ownership is fine in general but unnecessary here, unless you explicitly want to allow nullptr
s (in which case, yes, use a raw pointer).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With