3 ms·
While we're doing code review, in general there's no reason to pass a shared_ptr, even by const ref, unless you need to (e.g. if you might copy the shared_ptr w
by millimeterman 4y ago
While we're doing code review, in general there's no reason to pass a shared_ptr, even by const ref, unless you need to (e.g. if you might copy the shared_ptr within the function). It's better to pass a raw pointer to indicate that the function only requires a temporary non-owning view into the data.
static int add_numbers(Data *p) {
return p->a + p->b;
}
- Kranar 4y agoIt's best to pass a reference to specify that the argument shall not be null. Also avoid writing static functions in C++, make use of anonymous namespaces. namespace { int add_numbers(const Data& p) { return p.a + p.b; } }
- millimeterman 4y agoAgreed, though at some point we run up against the inherent triviality of the example :P.
- vasama 4y agoThere's no reason to avoid static functions. It's just a better syntax and it's a shame it cannot be applied to class declarations as well.