开发者

'const Name &var' or 'const Name& var'? [duplicate]

This question already has answers here: Closed 12 years ago.

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 o开发者_如何学Pythonf 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.


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.)


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.


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


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.


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.


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".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜