我想有一个类的私有静态常量(在这种情况下是一个形状工厂)。
我想要这样的东西。
class A {
private:
static const string RECTANGLE = "rectangle";
}
不幸的是,我从c++ (g++)编译器得到了各种各样的错误,比如:
ISO c++禁止初始化
“矩形”成员
非整型静态数据成员' std::string '的类内初始化无效
错误:使“矩形”静态
这说明这种构件设计是不符合标准的。如何在不使用#define指令的情况下获得私有文字常量(或者公共常量)(我想避免数据全局的丑陋!)
任何帮助都是感激的。
你必须在类定义之外定义你的静态成员,并在那里提供初始化式。
第一个
// In a header file (if it is in a header file in your case)
class A {
private:
static const string RECTANGLE;
};
然后
// In one of the implementation files
const string A::RECTANGLE = "rectangle";
您最初尝试使用的语法(类定义中的初始化式)只允许整型和枚举类型。
从c++ 17开始,你有另一个选项,这与你最初的声明非常相似:内联变量
// In a header file (if it is in a header file in your case)
class A {
private:
inline static const string RECTANGLE = "rectangle";
};
不需要额外的定义。
快进到2018年和c++ 17。
do not use std::string, use std::string_view literals
please do notice the 'constexpr' bellow. This is also an "compile time" mechanism.
no inline does not mean repetition
no cpp files are not necessary for this
static_assert 'works' at compile time only
using namespace std::literals;
namespace STANDARD {
constexpr
inline
auto
compiletime_static_string_view_constant() {
// make and return string view literal
// will stay the same for the whole application lifetime
// will exhibit standard and expected interface
// will be usable at both
// runtime and compile time
// by value semantics implemented for you
auto when_needed_ = "compile time"sv;
return when_needed_ ;
}
};
以上是一个适当的、合法的标准c++公民。它可以很容易地涉及到任何和所有std::算法、容器、实用程序等。例如:
// test the resilience
auto return_by_val = []() {
auto return_by_val = []() {
auto return_by_val = []() {
auto return_by_val = []() {
return STANDARD::compiletime_static_string_view_constant();
};
return return_by_val();
};
return return_by_val();
};
return return_by_val();
};
// actually a run time
_ASSERTE(return_by_val() == "compile time");
// compile time
static_assert(
STANDARD::compiletime_static_string_view_constant()
== "compile time"
);
享受标准c++吧