0

Possible Duplicate:
What makes more sense - char* string or char *string?

Sorry if this is a silly question, I am new to these things :-)

I am working on a C++ code base which uses the following reference conventions:

  • const Name &var
  • const Name& var

As far as I'm aware, they mean the same thing.

Which of these is either of these preferred, or mandated?

I don't like the ambiguity of having to choose one or the other.

The closest I have found to this answer is on http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Reference_Arguments , which uses the &var layout.

Community
  • 1
  • 1
  • If you don't like making decisions about coding style, then you're not going to like c++ or any of the related languages(c,java,c#,etc...). But if you're going to follow somebody's standards, Google is not a bad way to go. – Benjamin Lindley Oct 11 '10 at 00:22
  • I prefer `Type const& var` because it keeps the two modifiers of `Type` together. – MSalters Oct 11 '10 at 11:26

6 Answers6

3

From the language or compiler perspective both are exactly same. For a group project, you have to use a group style. For personal project, you are free to make any choice.

Though it is hard, but on topics like these, I always try to remember Rule# 0 from C++ Coding Standards: 101 Rules, Guidelines, Best Practices:

Don't sweat the small stuff. (Or: Know what not to standardize.)

If I have flexibility to choose, then personally, I always having prefer having spaces on either side of '*', '&', '=' etc. and so I write it as

const Name & var;

(Also, I never declare two variables in one line.)

Arun
  • 19,750
  • 10
  • 51
  • 60
2

The two are equivalent. The difference is purely one of style.

const Name &var is useful if you find yourself declaring multiple variables in a line:

const Name &var, foo, &bar;

Foo is a Name, but var and bar are references to a Name.

However, declaring multiple variables in one line is generally frowned on.

const Name& var makes it clearer that we're dealing with a reference type (since the reference symbol is located with the type name).

So, pick which style you prefer, and be consistent. Using a consistent style is far more important than which style you use.

Tim
  • 8,912
  • 3
  • 39
  • 57
1

As thyrgle says, if there is common style in use for a project or set of files, use that style. But if you're free to choose, then I think T const& v; is preferable. The const placement because that generalizes to more complicated declarations with more than one const, and the & placement because in C++ the focus is on types and because it makes less natural to try to declare two or more variables in one declaration. :-)

Cheers,

– Alf

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

C++ doesn't care about whitespaces as long as you don't dabble in templates and operators, which seem to be a while away for you. In short, they mean the same thing, personally I prefer "const Name& var".

dutt
  • 7,909
  • 11
  • 52
  • 85
  • Actually, C++ does care a bit about whitespace inside and outside templates. Not a big difference actually. `a && b` is not the same as `a & &b`. `a >> b` is not the same as `a > >b`. – MSalters Oct 11 '10 at 11:29
  • Ah, true. templates and operators then, thanks :) edited. – dutt Oct 11 '10 at 21:55
0

It's exactly the same for compiler.

Use the you like best one.

I prefer "const Name &var" because it looks better for two or more vars: "const Name &var, &var2, var3;" for example.

0

C++ doesn't care about white spaces so each does exactly the same thing. But, you should use which ever one your company, or project host is using. For example: If I was working for Google I would use the &var layout because you need to keep everything consistent with what their style is.