在C ++ 11中,我们鼓励对变量类型使用auto,
这在初始化类和向量之类的类型时也适用吗?
我的意思是我们应该写以下内容:
auto a = 10;
auto b = MyClass();
auto c = vector<int>{1, 2, 3};
代替:
auto a = 10;
MyClass b;
vector<int> c = {1, 2, 3};
auto
只是简化诸如
VeryLongClassName *object = new VeryLongClassName();
现在它将
auto *object = new VeryLongClassName();
没有理由写
auto a = 10;
auto b = MyClass();
auto c = vector<int>();
因为它比它更长更难读
int a = 10;
MyClass b;
vector<int> c;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句