3 ms·
One interesting thing is that the QT framework prefers the use of pointers over reference variables. There's actually some reasonable logic here. Pointer syntax
by joe_the_user 13y ago
One interesting thing is that the QT framework prefers the use of pointers over reference variables. There's actually some reasonable logic here. Pointer syntax clearly shows you are acting on something in a different way than you act on an "ordinary" object. Reference object syntax is the same as ordinary object syntax but you are making changes that will go beyond the local scope and that is thus less obvious.
With this approach, you can still allocate objects on the stack - you then take their address and pass that to a subroutine.
- acchow 13y agoPresumably, 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.