<分区>
哪种写法最好:
string* str
或者:
string *str
其中一个有副作用吗?
谢谢
标签 c++ c pointers coding-style
<分区>
哪种写法最好:
string* str
或者:
string *str
其中一个有副作用吗?
谢谢
最佳答案
选择第二个的原因是当您一次声明多个变量时:
string *str, *foo;
string* str, foo;
这两行是不同的,第一行声明了一个指针,而第二行声明了一个指向string
和一个string
的指针。
[FredOverflow 的评论] 这个问题可以通过一些模板魔术来解决,不过 ;-)
template <typename T>
struct multiple
{
typedef T variables;
};
multiple<string*>::variables str, foo;
关于c++ - 正确的 C 指针表示法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4071068/