3 ms·
Why do you feel that it's more error-prone? function(a); vs. a->function(); I can accept that some think the second version looks nicer. But I don't buy tha
by colonwqbang 5y ago
Why do you feel that it's more error-prone?
function(a);
vs.
a->function();
I can accept that some think the second version looks nicer. But I don't buy that we should have to jump through hoops like pimpl just to get there for non virtual calls.
- ncmncm 5y agoNobody cares how it looks. What matters is that your "a" is a naked pointer. Pointers have reference semantics, not value semantics, so are inherently error-prone. A pointer might be null. A pointer might be already freed. A pointer might be left over from some other use. A PImpl object is a value, with value semantics. It owns the wrapped pointer, and whatever that points at. When it goes out of scope, it cleans up. If you throw, it cleans up. When it is copied, the right thing happens, if that is allowed, or cannot happen, if not. When it is passed to a function, the right thing happens. When it is returned from a function, the right thing happens. The "right thing", in each case, is visible in exactly one place in the source code, and nowhere else. Nothing using it can do any of it wrong. This is all really fundamental stuff.