Mastering C++ Programming
上QQ阅读APP看书,第一时间看更新

Inline variables

Just like the inline function in C++, you could now use inline variable definitions. This comes in handy to initialize static variables, as shown in the following sample code:

#include <iostream>
using namespace std;

class MyClass {
private:
static inline int count = 0;
public:
MyClass() {
++count;
}

public:
void printCount( ) {
cout << "\nCount value is " << count << endl;
}
};

int main ( ) {

MyClass obj;

obj.printCount( ) ;

return 0;
}

The preceding code can be compiled and the output can be viewed with the following commands:

g++-7 main.cpp -std=c++17
./a.out

The output of the preceding code is as follows:

Count value is 1