初始化 static std::vector时出现问题在同一个类构造函数中
如何在类中正确声明静态向量? 目前,由于向量初始化太晚,我在一行上崩溃了。
示例一:
#include "stdafx.h"
#include <vector>
class A{
private:
int aValue;
public:
static std::vector<A*> listOfA;
A(int i)
{
aValue = i;
A::listOfA.push_back(this); // !!! HERE crash in release mode only, in debug mode items add to vector, but remove when vector initialize
}
};
A testA(1);
std::vector<A*> A::listOfA;
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
示例二:
classA.h
#include <vector>
class A{
private:
int aValue;
public:
static std::vector<A*> listOfA;
A(int i);
};
classA.cpp
#include "stdafx.h"
#include "classA.h"
std::vector<A*> A::listOfA;
A::A(int i)
{
aValue = i;
A::listOfA.push_back(this); // !!! HERE crash in release mode only, in debug mode items add to vector, but remove when vector initialize
}
main.cpp
#include "stdafx.h"
#include "classA.h"
A testA(1);
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
在示例 2 中,如果项目中的 cpp 文件具有此顺序(编译顺序),则一切正常: A类.cpp main.cpp
如果命令这个,我们会崩溃: 主程序 A类.cpp
How to correct declare static vector in class?
Currently I have crash on one line because vector initialize too late.
Sample one:
#include "stdafx.h"
#include <vector>
class A{
private:
int aValue;
public:
static std::vector<A*> listOfA;
A(int i)
{
aValue = i;
A::listOfA.push_back(this); // !!! HERE crash in release mode only, in debug mode items add to vector, but remove when vector initialize
}
};
A testA(1);
std::vector<A*> A::listOfA;
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Sample two:
classA.h
#include <vector>
class A{
private:
int aValue;
public:
static std::vector<A*> listOfA;
A(int i);
};
classA.cpp
#include "stdafx.h"
#include "classA.h"
std::vector<A*> A::listOfA;
A::A(int i)
{
aValue = i;
A::listOfA.push_back(this); // !!! HERE crash in release mode only, in debug mode items add to vector, but remove when vector initialize
}
main.cpp
#include "stdafx.h"
#include "classA.h"
A testA(1);
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
In sample 2 if cpp files in project have this order(compilation order) all works fine:
classA.cpp
main.cpp
If order this, we have crash:
main.cpp
classA.cpp
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
引用自: http://www.parashift.com/c++- faq-lite/ctors.html#faq-10.14
这个问题有很多解决方案,但一个非常简单且完全可移植的解决方案是用全局函数 listOfA() 替换全局对象 listOfA,该函数通过引用返回对象。
由于静态局部对象是在第一次控制流过其声明时构造的,因此上面的 new listOfA() 语句只会发生一次:第一次调用 listOfA() 时。每个后续调用都将返回相同的对象。然后您要做的就是将 listOfA 的用法更改为 listOfA():
这称为“首次使用时构造”习语,因为它就是这样做的:全局 Fred 对象是在第一次使用时构造的。
这种方法的缺点是对象永远不会被破坏。还有另一种技术可以解决这个问题,但需要谨慎使用,因为它可能会产生另一个(同样令人讨厌的)问题。
[编辑] 抱歉,nbt,没有看到您已经链接到常见问题解答。他值得称赞[/编辑]
quoted from: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14
There are many solutions to this problem, but a very simple and completely portable solution is to replace the global object listOfA, with a global function, listOfA(), that returns the object by reference.
Since static local objects are constructed the first time control flows over their declaration (only), the above new listOfA() statement will only happen once: the first time listOfA() is called. Every subsequent call will return the same object. Then all you do is change your usages of listOfA to listOfA():
This is called the Construct On First Use Idiom because it does just that: the global Fred object is constructed on its first use.
The downside of this approach is that the object is never destructed. There is another technique that answers this concern, but it needs to be used with care since it creates the possibility of another (equally nasty) problem.
[Edit] Sorry nbt, didn't see that you already linked to the faq. He deserves the credit[/Edit]