c++ - 初始化在类的私有(private)成员中声明的静态容器数据

标签 c++ vector static containers

我有一个包含私有(private)成员 (struct) 的类,如下所示:

enum indices {
   zero,
   one, 
   two,
   ...,
   N
};

class myclass {
  ...
  ...
private: 
  struct impl;
  impl* p_impl;
};

然后,在实现中,我在结构中定义了一个静态容器( vector ),如下所示:

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 而不会遇到下面描述的错误?

附加信息:

当我尝试在实现中但在结构和任何类成员之外(以及在设置 vector 大小的赋值之后)初始化它时,如下所示:

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 容器做同样事情的例子?一如既往地欣赏任何想法和想法。谢谢!

最佳答案

Is the place where I have initialized the static container appropriate?

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

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

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

If I moved the initializing of v before allocating p_impl, would it result in run-time errors? In other words, is there an order in which memory is assigned to the static members?

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

Also, how can I make my container to be static const without running into the errors described below?

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

std::vector<std::string> myclass::impl::v = { str0, str1, str2, ... };

关于c++ - 初始化在类的私有(private)成员中声明的静态容器数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11236065/

相关文章:

c++ - std::vector 结构的 MPI BCast(广播)

c++ - 体系结构 x86_64 的 undefined symbol 静态类成员错误

java - 在 Java 中访问静态字段的正确方法是什么?

c# - 静态构造函数和继承

c++ - 是否可以在运行时修改 PYTHONPATH?

c++ - 使用抽象类的子类专门化模板

c++访问对象 vector 中对象的元素

c++ - 使用 unique_ptr 缓存位置

c++ - LLVM CreateFCmpONE 等效于整数

c++ - 搜索给定单词的字符串并返回 bool 值