我想有一个类的私有静态常量(在这种情况下是一个形状工厂)。

我想要这样的东西。

class A {
   private:
      static const string RECTANGLE = "rectangle";
}

不幸的是,我从c++ (g++)编译器得到了各种各样的错误,比如:

ISO c++禁止初始化 “矩形”成员 非整型静态数据成员' std::string '的类内初始化无效 错误:使“矩形”静态

这说明这种构件设计是不符合标准的。如何在不使用#define指令的情况下获得私有文字常量(或者公共常量)(我想避免数据全局的丑陋!)

任何帮助都是感激的。


当前回答

在c++ 17中,你可以使用内联变量:

class A {
 private:
  static inline const std::string my_string = "some useful string constant";
};

注意,这与深渊不同。7的回答:它定义了一个真正的std::string对象,而不是const char*

其他回答

你必须在类定义之外定义你的静态成员,并在那里提供初始化式。

第一个

// 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";
};

不需要额外的定义。

在c++ 11中,你现在可以做到:

class A {
 private:
  static constexpr const char* STRING = "some useful string constant";
};

当前标准只允许对静态常量整型进行这样的初始化。所以你需要按照AndreyT解释的去做。但是,这将在下一个标准中通过新的成员初始化语法实现。

在c++ 17中,你可以使用内联变量:

class A {
 private:
  static inline const std::string my_string = "some useful string constant";
};

注意,这与深渊不同。7的回答:它定义了一个真正的std::string对象,而不是const char*

可能的只是做:

static const std::string RECTANGLE() const {
    return "rectangle";
} 

or

#define RECTANGLE "rectangle"