【问题标题】:initializing static container data declared in private member of a class初始化在类的私有成员中声明的静态容器数据
【发布时间】: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


    【解决方案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, ... };
    

    【讨论】:

      猜你喜欢
      • 2014-03-31
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 2012-07-10
      • 1970-01-01
      • 2012-02-18
      • 2015-02-18
      相关资源
      最近更新 更多