3 ms·
Presumably, this pointer vs. reference debate is only relevant in C and no longer has a place in C++ with the use of unique_ptr, shared_ptr, std::move, etc?
by acchow 13y ago
Presumably, this pointer vs. reference debate is only relevant in C and no longer has a place in C++ with the use of unique_ptr, shared_ptr, std::move, etc?
- ori_b 13y agoC has no references -- 'type& param' -- so there is no debate. This is a topic exclusive to C++, and isn't affected by smart pointers, which overload all the pointer operators in order to act like regular pointers, only with some sort of lifetime management.
- EpicEng 13y agoConsidering that C has no notion of references, no, I cannot agree...
- joe_the_user 13y agoIn the Qt idiom, these smart pointers are irrelevant (as mentioned in the other poster). Here, when you are passing a pointer, it's going to be a pointer to an object on the stack which is always going to be good - the pointer argument shows you that you are dealing with something outside the functions local scope, the variable allows you pass information back, etc. Qt also has used its own smart pointers for a while but these generally are declared within some container class, so the idiom basically allows you to do allocation entirely within containers and so never use the new operator directly. Now, there may be situations where you have a complex chain of multiple connections that don't fit to an existing container. The sloppy way then is to use new and the good way to write a custom container. The idiom can also when dealing external libraries that allocate memory directly or somehow demand you use the new operator.