C++类型转换顺序/正确的代码格式?
可能的重复:
声明指针;星号位于类型和名称之间的空格左侧还是右侧?
const int* 和 const int* 之间有什么区别, const int * const, int const *
我一直想知道两者之间有什么区别:
float const &var
const float &var
其中哪一个是编写代码的正确方法? (包括上面的例子):
float const& var
float const &var
float const & var
和指针:
float * var
float *var
float* var
我总是把特殊标记放在变量名之前,感觉最合乎逻辑。这是正确的方法吗?
Possible Duplicates:
Declaring pointers; asterisk on the left or right of the space between the type and name?
what is the difference between const int*, const int * const, int const *
I've been wondering what is the difference between:
float const &var
const float &var
And which one of these is the correct way of writing the code? (including the above example):
float const& var
float const &var
float const & var
and with pointers:
float * var
float *var
float* var
I always put the special marks just before the variable name, feels most logical. Is that the correct way ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一切都同样有效。没有一种方法是正确的;您应该选择您认为最可读的方式(对于您自己的代码)或遵循流行的风格(如果与其他人一起工作)。
根据我的经验,将
const
放在第一位(例如,const float &
而不是float const &
)更为常见。&
和*
的位置取决于程序员;根据我的经验,没有什么选择比其他选择更常见。All are equally valid. There is no one correct way; you should do whichever you find most readable (for your own code) or follow the prevailing style (if working with others).
Putting
const
first (e.g.,const float &
rather thanfloat const &
) is more common in my experience.The positioning of
&
and*
depends on programmer; no choice seems more common than any other, in my experience.关于你的第一个问题:
参考这个:
http://www.parashift.com/c++- faq-lite/const- Correctness.html#faq-18.8
当谈到指针时:
是最好的方法。
原因是当您有多个指针变量时,它不会给人错误的印象。
例如:
如果您需要 3 个指针变量,则正确。
与可能错误的代码相比:
Regarding your first question:
Refer this:
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.8
When it comes to pointers:
is the best way.
The reason is that it doesn't give a wrong impression when you have multiple pointer variables.
For example:
Is correct if you are wanting 3 pointer variables.
Compared to the possibly wrong code:
您公开的第一个代码片段提醒了 const 正确性: http:// en.wikipedia.org/wiki/Const- Correctness#C.2B.2B_syntax。
在其他两个代码片段中,任何变体都具有相同的效果。
有关指针和 const 关键字的更多信息,请参阅以下文档: http:// www.cplusplus.com/doc/tutorial/pointers/
The first code snippet you exposed reminds of the const correctness: http://en.wikipedia.org/wiki/Const-correctness#C.2B.2B_syntax.
In the other two code snippets any of variants have the same effect.
For further information about pointers and the const keyword read also the following piece of documentation: http://www.cplusplus.com/doc/tutorial/pointers/