我想在类
在
在
Foo
中初始化一个 static const std :: vector
,使其为 {0,1,2,3,...,n}
,其中n
是根据下面的 enum Last
中的 Last
值在编译时已知的。目标是让 Foo :: all
包含 Fruit
枚举的所有值。在
foo.h
中:enum Fruit { Apple, Orange, Banana, ..., Last };
class Foo {
public:
static const vector<int> all;
};
在
foo.cpp
中:// initialization of Foo::all goes here.
=
符号。 - Jonathan WakelyBigInt i = 5;
和BigInt i(5);
这两种方式之外,还有什么区别吗?我从来没有听说过对此有强烈意见的人。 - chris=
符号等同于vector<int> v = vector<int>{0, 1, 2, 3, ..., n}
。@chris的区别在于一个需要非显式构造函数和可访问的复制构造函数(显然,vector
有,但并非所有类型都有)。例如,std::unique_ptr<int> p = new int;
是无效的,但std::unique_ptr<int> p(new int);
是有效的。 - Jonathan Wakely