更新时间:2022-10-15 15:50:06
像其他人说的,ISO C ++不支持。但你可以解决它。只需使用std :: vector代替。
int * a = new int [N]
//填充
类C {
const std :: vector< int> v;
public:
C():v(a,a + N){}
};
I have the following class in C++:
class a {
const int b[2];
// other stuff follows
// and here's the constructor
a(void);
}
The question is, how do I initialize b in the initialization list, given that I can't initialize it inside the body of the function of the constructor, because b is const
?
This doesn't work:
a::a(void) :
b([2,3])
{
// other initialization stuff
}
Edit: The case in point is when I can have different values for b
for different instances, but the values are known to be constant for the lifetime of the instance.
Like the others said, ISO C++ doesn't support that. But you can workaround it. Just use std::vector instead.
int* a = new int[N];
// fill a
class C {
const std::vector<int> v;
public:
C():v(a, a+N) {}
};