【问题标题】:Initialize a static const class member with no parameters in constructor of a custom class in C++在 C++ 中自定义类的构造函数中初始化一个没有参数的静态 const 类成员
【发布时间】:2018-04-02 13:49:49
【问题描述】:

如何在 C++ 中初始化自定义类的静态 const 类成员?

这是我目前尝试过的:

头文件:

class A
{
private:
static const B b;
};

源文件:

const B A::b;

类的 B 构造函数没有参数。

该方法不起作用。 b 在源文件中变为红色下划线,并写在那里const member "A::b" requires an initializer

【问题讨论】:

    标签: c++ class static initialization member


    【解决方案1】:

    您是否为 B 提供了默认构造函数?

    class B
    {
    public:
        B() {}
    };
    

    在这里工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多