我有MyClass.h文件中的類:是有可能把非(靜態常量積分)類型在C++ Header.h文件
// MyClass.h
#ifndef __MY_CLASS_H__
#define __MY_CLASS_H__
#include <string>
class MyClass
{
static const std::string MyStaticConstString; // I cannot initialize it here, because it's not an integral type.
};
// OK, let's define/initialize it out side of the class declaration
// static
const std::string MyClass::MyStaticConstString = "Value of MyStaticConstString";
#endif
的問題是,編譯器會抱怨「多重定義」如果該文件包含多次。
所以我必須將MyStaticConstString
的定義移動到MyClass.cpp文件。但是如果MyClass
是庫的一部分,我希望我的用戶在MyClass.h文件中查看const靜態值,這是合理的,因爲它是一個靜態常量值。
我該怎麼辦?我希望我明確自己。
謝謝。
彼得
在C#中,您始終可以將類初始化放在類聲明中。 – 2011-05-23 22:33:00
@Neil,我的意思是C++成員初始化規則真的令人沮喪,我個人認爲。我沒有從這些規則中受益。讓我知道他們是否有。 – 2011-05-24 21:07:02