3

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?

weggo
  • 31
  • 1
  • 4
    "Even Microsoft's directx uses pointers more than the safer option" Which DirectX APIs are you looking at? Most of them are C APIs and C does not have references. – James McNellis Aug 02 '11 at 22:17
  • 1
    Possibly duplicate of (or answered by) http://stackoverflow.com/q/57483/677667 – Chris A. Aug 02 '11 at 22:19
  • @Chris: That is not a duplicate. http://meta.stackexchange.com/questions/95799/closing-as-duplicate-when-the-answers-are-duplicates – Billy ONeal Aug 02 '11 at 22:29

3 Answers3

2

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.

Omri Barel
  • 9,182
  • 3
  • 29
  • 22
  • 1
    I don't think it's really an indication of quality. The Google Guide is designed to make code that works well with Google's existing, mostly C, codebase, not necessarily to be the best for new code. For example, they prohibit C++ exceptions for this reason. – Billy ONeal Aug 02 '11 at 23:23
1

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.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
0

There are many reasons why code that you think would be impeccable isn't.

In the Open source arena one of the problems is that there are many people contributing there time for free with different levels of skill, so you often end up with a mish mash of styles some of which include C with classes.

In large project like dx10 the are many reasons as to why pointers are used other reference but often has a lot to with making it as easy to integrate with 'pure C' as possible; which doesn't have reference types. Or is simply a C library wrapped in a class abstraction.

Non the less just because you see 'bad' code around you is no reason to write it yourself.

111111
  • 15,686
  • 6
  • 47
  • 62