初始化基本类型(如int或指针)时,可以使用复制初始化或直接初始化 。
int a = 10; int b(10);虽然后一种方式是为具有构造函数的对象提供的,但我没有看到人们将它用于基元。 我理解使用'='运算符是“更自然”(特别是数字),但有人在现实代码中编写代码:
for (int i(0); i < 5; ++i) { cout << i << endl; }谢谢。
编辑:问题是关于编码风格和最佳实践而不是技术实现。
When initializing primitive types like int or pointers one can use either copy-initialization or direct-initialization.
int a = 10; int b(10);Although the latter way is preffered for objects with constructors, I don't see people using it for primitives. I understand that it is kind of "more natural" (especially for numbers) to use the '=' operator but is there anybody writing things like in real-life code:
for (int i(0); i < 5; ++i) { cout << i << endl; }Thanks.
EDIT: The question asks about coding styles and best practices rather than technical implementation.
最满意答案
有些人这样做是为了保持一致。
在模板内部,代码可以是
for (T i(0); i < 5; ++i) { cout << i << endl; }并且以任何方式编写它将使编码风格保持一致。
Some people do this to be consistent.
Inside a template, the code could be
for (T i(0); i < 5; ++i) { cout << i << endl; }and writing it that way everywhere would make the coding style consistent.
更多推荐
发布评论