As I've been coding C/C++ for about a year now, I've tried to learn the preferred ways for writing good OO and C++ code. That means, when coding, I always look for the best ways when implementing something, rather than just typing something and checking if it works. I've told that in C++ you should use references instead of raw pointers when passing arguments to functions, to make it more safe. So that would be considered as better coding. Although, in many high quality opensource projects(which are C++) they use pointers instead of references. All arguments, returns and stuff are pointers rather than safer references. Even Microsoft's directx uses pointers more than the safer option. I cannot see any benefits by using pointers instead of references, actually I think raw pointers are worse than references(in situations where dynamic alloc isn't needed of course).
So onto my question: why most of the opensource projects use pointers, when there are better ways available?
Not sure if that's an indication of quality or not, but according to Google's C++ Style Guide,
All parameters passed by reference must be labeled const.
...
In fact it is a very strong convention in Google code that input arguments are values or const references while output arguments are pointers.
In "many high quality open source projects", ideomatic C++ is not used. Many such libraries view C++ as C with a few bells and whistles added, rather than as it's own language.
Without a specific library in question though, it's difficult to say anything about use of references versus no use of references. If you're exposing a C API, then of course you're forced to use pointers, because C does not have references.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With