具有静态存储持续时间的对象的C++级联破坏
C++ Cascading destructions of objects with static storage duration
此链接介绍了具有静态存储持续时间的对象的级联破坏是C++中流行的未定义行为。它到底是什么?我不能理解。如果用一个简单的C++程序来解释它,它会更好。非常感谢你的帮助。感谢
静态销毁.h
#include <vector>
class first
{
public:
static std::vector<int> data;
public:
first();
~first();
};
class second
{
public:
static std::vector<int> data;
public:
second();
~second();
};
class container
{
public:
static first f;
static second s;
};
静态破坏.cpp
#include <iostream>
#include "static_destruction.h"
first::first()
{
data = {1, 2, 3, 4};
}
first::~first()
{
std::cout << second::data.size() << std::endl;
}
std::vector<int> first::data;
second container::s;
int main(void)
{
}
静态破坏2.cpp
#include <iostream>
#include "static_destruction.h"
second::second()
{
data = {1, 2, 3, 4, 5, 6};
}
second::~second()
{
std::cout << first::data.size() << std::endl;
}
std::vector<int> second::data;
first container::f;
由于编译单元中静态对象的破坏顺序是未定义的(实际上是未定义,但结果是相同的,因为破坏顺序是构造的相反顺序),在我的机器上,根据我编译文件的顺序,它会给我不同的输出:
$> g++ -std=c++11 static_destruction.cpp static_destruction2.cpp
$> ./a.out
0
4
和
$> g++ -std=c++11 static_destruction2.cpp static_destruction.cpp
$> ./a.out
0
6
我相信这就是中未定义行为的含义
具有静态存储持续时间的物体的级联破坏
相关文章:
- 只有级联分类器会发出未定义的引用错误
- 裁剪并显示我从哈尔级联获得的图像
- 每次b/c程序无法加载级联时,使用harr级联的人脸识别都会失败
- C++ 为什么要级联共享对象链接
- 使用打开级联读取步骤文件
- 如何使用所有其他文件信息来编译代码,例如HAAR级联训练的权重的XML文件
- 为什么 haar 级联非常慢 opencv c++.
- 级联功能调用
- 通过级联呼叫来递增值
- 复制构造函数和级联功能调用
- 如何实现分类比较器,以级联相关对象
- CUDA:所有矢量元素的级联求和
- 用于人脸检测的Haar级联xml文件代码解释OpenCV
- 恒定时间级联计算是否可行
- 级联文件的iOS opencv filepath
- OpenCV对象在训练级联后检测算法会冻结
- 返回引用和使用级联函数
- 在黑莓 10 级联 Beta 4 SDK 中显示带有给定号码的拨号盘
- BlackBerry 10级联-如何导航到另一个CPP类
- 返回 *this,尝试级联,并且不返回引用