I am very new to C++ smart pointers and I am having a hard time understanding this advice around using them for function arguments.
"C++ Coding Standards: 101 Rules, Guidelines, and Best Practices " says that these are the reasons to pass in smart pointer.
Prefer passing by (smart) pointer
- if the argument is optional (so callers can pass null as a “not available” or “don’t care” value)
- or if the function stores a copy of the pointer
- or otherwise manipulates ownership of the argument.
Can somebody please give me examples of each one of them and why not using a smart pointer would be a bad idea in the cases?
Prefer passing by (smart) pointer [when ...]
I think you are misreading the coding standard. You read this as "these are the reasons to use a smart pointer". What the author intended is "these are the reasons to use some kind of pointer, which might optionally be a smart pointer."
Prefer passing by (smart) pointer
Note how "smart" is in parenthesis?
What the person is talking about here is passing by pointer vs. reference. Smart is there in parenthesis because you'd follow similar rules when working in a team that prefers smart pointers.
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