gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:01:49 27 4
gpt4 key购买 nike

我有一个包含私有(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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com