0

我有一个带有私有成员(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;
}

... 

我的问题是:我初始化静态容器的地方是否合适?v如果我在分配之前移动了初始化p_impl,会导致运行时错误吗?换句话说,内存分配给静态成员是否有顺序?另外,我怎样才能让我的容器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(似乎是一个自然的地方)的构造函数中。此外,如果我尝试将容器制作成 a 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容器做同样事情的例子?一如既往地欣赏任何想法和想法。谢谢!

4

1 回答 1

1

我初始化静态容器的地方合适吗?

是的,类的静态数据成员需要在命名空间范围内进行定义。但是这些

myclass::impl::v[zero] = str0;
myclass::impl::v[one] = str1;
...
myclass::impl::v[N] = strN;

是表达式,它们在全局范围内是不允许的。编译器试图将它们解析为声明,因此出现错误:“无法分配常量大小为 0 的数组”。您需要在成员函数中移动此逻辑。

如果我在分配 p_impl 之前移动了 v 的初始化,会导致运行时错误吗?换句话说,内存分配给静态成员是否有顺序?

静态成员具有外部链接,因此可以保证在 main() 开始之前完全构造它们。

另外,如何使我的容器成为静态 const 而不会遇到下面描述的错误?

您只能初始化一个const变量,以后不能更改它。在 C++11 中你可以做

std::vector<std::string> myclass::impl::v = { str0, str1, str2, ... };
于 2012-06-27T23:21:27.650 回答