由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - static initialization dependency c++ (转载)
相关主题
再问C++有面过knight的吗
singleton pattern problemC++ Q68: initialization (skillport)
一个电面问一个c++问题关于reference vs. pointer
最新某公司onsite面试题bloomberg 问题: C++ construct 时用 new 没"()"
C++: 如何对const data member做assignment?发几个C++面试题,senior的职位
这个拷贝构造函数有什么问题?A question about C++. Thanks.
搞不清C++里的constant expression攒rp,电面题目
Java/C++ 的牛人们给看看这个Interview的Home test (急)今天最后几个c语言编程的问题
相关话题的讨论汇总
话题: static话题: here话题: c++话题: dependency
进入JobHunting版参与讨论
1 (共1页)
r**u
发帖数: 1567
1
【 以下文字转载自 Programming 讨论区 】
发信人: raou (raou), 信区: Programming
标 题: static initialization dependency c++
发信站: BBS 未名空间站 (Wed Feb 6 13:38:35 2013, 美东)
Here's a common scenario: source file first.cpp define a static data member
of a class and provided an initializer for it. Source file second.cpp #
includes first.h and attempts to examine the value of the static data member
. When you execute that statement, the program crashes.The reason is the
compiler doesn't guarantee that C::i is initialized before it is referenced
in the second.cpp. Here is example:
//----first.h
class C
{
public:
static const int i;
};
//----first.cpp
#include "first.h"
const int C::i=5;
//----second.cpp
#include "first.h"
int MIN=C::i; // may crash here
Questions:
1. Do we have this problem in C too or only in C++? I have never noticed
this problem in C.
2. This seems a pretty serous problem. How to avoid it? E.g., if we use
singleton, how to make sure that the instance is initialized before use it?
1 (共1页)
进入JobHunting版参与讨论
相关主题
今天最后几个c语言编程的问题C++: 如何对const data member做assignment?
问个static STL container的问题这个拷贝构造函数有什么问题?
c++ interview question搞不清C++里的constant expression
这种牛逼的写法是实现Map接口的匿名内部类吗?Java/C++ 的牛人们给看看这个Interview的Home test (急)
再问C++有面过knight的吗
singleton pattern problemC++ Q68: initialization (skillport)
一个电面问一个c++问题关于reference vs. pointer
最新某公司onsite面试题bloomberg 问题: C++ construct 时用 new 没"()"
相关话题的讨论汇总
话题: static话题: here话题: c++话题: dependency