简体   繁体   中英

Initialize a static const class member with no parameters in constructor of a custom class in C++

How can I initialize a static const class member of a custom class in C++?

Here is what I tried so far:

Header file:

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

Source file:

const B A::b;

Class`s B constructor has no parameters.

The approach does not work. The b becomes red underlined in the source file and it is written over there const member "A::b" requires an initializer .

Did you provide a default constructor for B?

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

It worked here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM