in-class initialization of static data member
时间: 2024-03-26 14:39:09 浏览: 107
In-class initialization of static data member is a feature in C++ that allows you to initialize a static data member inside the class definition. This means that the static data member will be initialized before any object of the class is created.
To initialize a static data member in-class, you must use the following syntax:
```c++
class MyClass {
public:
static int myStaticData = 42; // in-class initialization of static data member
};
```
In this example, the static data member `myStaticData` is initialized with the value `42` inside the class definition.
Note that in-class initialization of static data member can only be done for `const` integral data types and enums. For non-const static data members, you need to initialize them outside the class definition.
阅读全文