Tom Ritchford
1 min readJun 18, 2020

--

There are very, very few good reasons to pass a raw pointer. T* gives you really no information about the pointer at all. Does it need to be deleted? Can it be null? You just don’t know. It’s an incitement to undefined behavior.

Nullable pointers are also a bad idea. We have std::optional for that purpose these days, and it doesn’t require an extra pointer. Nullable pointers are the quickest way to undefined behavior. If a nullable pointer comes in from the outside, I usually catch it at the very top of my code and just never pass it down.

So your advice should really emphasize references and strongly de-emphasize pointers.

--

--

No responses yet