【发布时间】:2012-06-27 23:14:02
【问题描述】:
我有一个带有私有成员 (struct) 的类,如下所示:
enum indices {
zero,
one,
two,
...,
N
};
class myclass {
...
...
private:
struct impl;
impl* p_impl;
};
然后,在实现中,我在结构中定义了一个静态容器(向量),如下所示:
struct myclass::impl {
impl() : ... {
...
do_something;
...
}
...
...
static std::vector<std::string> v;
...
};
std::vector<std::string> myclass::impl::v = std::vector<std::string>(N); //-N is defined by an enum, say.
myclass::myclass() {
p_impl = new impl();
//-Is this where I should populate the static container?
impl::v[zero] = str0;
impl::v[one] = str1;
...
impl::v[N] = strN;
}
...
我的问题是:我初始化静态容器的地方合适吗?如果我在分配p_impl 之前移动了v 的初始化,会导致运行时错误吗?换句话说,内存分配给静态成员是否有顺序?另外,我怎样才能让我的容器成为static const 而不会遇到下面描述的错误?
附加信息:
当我尝试在实现中但在结构和任何类成员之外(以及在设置向量大小的赋值之后)初始化它时,如下所示:
std::vector<std::string> myclass::impl::v = std::vector<std::string>(N); //-N is defined by an enum, say.
myclass::impl::v[zero] = str0;
myclass::impl::v[one] = str1;
...
myclass::impl::v[N] = strN;
然后我在 Windows (VC++ 2008) 上遇到构建错误:
error C2466: cannot allocate an array of constant size 0
这导致我将初始化放在myclass 的构造函数中(似乎是一个自然的地方)。此外,如果我尝试将容器设置为 static const,则所有内容都会出现错误,如下所示。
error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xstring(914): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const std::basic_string<_Elem,_Traits,_Ax> &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xstring(919): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xstring(924): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
while trying to match the argument list '(const std::basic_string<_Elem,_Traits,_Ax>, const std::string)'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
有没有人用static const 容器做同样的事情的例子?一如既往地欣赏任何想法和想法。谢谢!
【问题讨论】:
标签: c++ vector static containers