使用常量变量问题定义替换

时间:2013-07-26 11:15:47

标签: c++ qt templates header constants

我正在创建一个组件,模板的泛化。对于创建必须使用字符串标识符。

我正在替换:

#define MYCOMPONENT_CONSTANT_IDENTIFIER        "ID value"

namespace myComponent
{
    static const QString constant_identifier = "ID value"
}

遵循一些编码标准(MISRA,......)。

这应该适用于C ++。我在Constants-only header file C++处查了一下。

此常量在组件“myComponent”的标头中定义,并包含在我的Indexer初始化和创建组件的标头中。这在更换时没有改变。

替换构建成功,但尝试运行失败。 分段错误取决于:

template<>
inline void TMyIndexer::Init()
{
  Map(...)
  //before
  //Map( ENUM_VAL, QSharedPointer<ITableFieldDefs>(new myComponent::TTableFieldDefs(MYCOMPONENT_CONSTANT_IDENTIFIER)) );
  Map( ENUM_VAL, QSharedPointer<ITableFieldDefs>(new myComponent::TTableFieldDefs(myComponent::constant_identifier)) );
  Map(...)
}

其中:

// TStaticFieldDefs<> implements ITableFieldDefs
typedef TStaticFieldDefs<myComponent::Fields> TTableFieldDefs;

//constructor
TStaticFieldDefs(QString id) : fId(id) {}

如果我上堆:

2。)qstring.h:内联QString :: QString(const QString&amp; other):d(other.d) {Q_ASSERT(&amp; other!= this); D-&GT; ref.ref(); }

1。)qatomic_x86_64.h:内联bool QBasicAtomicInt :: ref()

我认为模板概括,构造函数中的内联定义或其他我不知道的东西有问题。

欢迎任何解释。

我没有想法,我很乐意寻求帮助。

1 个答案:

答案 0 :(得分:2)

我的猜测是你试图从静态上下文中使用常量对象。 C ++标准规定静态对象初始化的顺序是未定义的。因此,您可以引用可能导致段错误的未初始化对象。

相关问题