官术网_书友最值得收藏!

  • Mastering C++ Programming
  • Jeganathan Swaminathan
  • 113字
  • 2021-07-02 18:28:47

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
主站蜘蛛池模板: 南澳县| 新乐市| 团风县| 西吉县| 澄迈县| 当涂县| 岳普湖县| 奉节县| 新疆| 通化市| 屯留县| 华容县| 庄河市| 星子县| 马山县| 彩票| 株洲县| 黄龙县| 朝阳县| 西乡县| 绥德县| 阿合奇县| 门源| 柳州市| 隆回县| 靖州| 清水河县| 涟源市| 会同县| 信阳市| 小金县| 汉中市| 昭通市| 广元市| 丹棱县| 鹤峰县| 吴江市| 绥棱县| 洛隆县| 澄迈县| 阳泉市|