- 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
推薦閱讀
- Spring 5.0 By Example
- 小創(chuàng)客玩轉(zhuǎn)圖形化編程
- DevOps for Networking
- 深入淺出Electron:原理、工程與實(shí)踐
- 數(shù)據(jù)庫(kù)系統(tǒng)原理及MySQL應(yīng)用教程
- Instant 960 Grid System
- oreilly精品圖書(shū):軟件開(kāi)發(fā)者路線圖叢書(shū)(共8冊(cè))
- 游戲程序設(shè)計(jì)教程
- 青少年學(xué)Python(第1冊(cè))
- Integrating Facebook iOS SDK with Your Application
- 編程與類型系統(tǒng)
- Java面向?qū)ο蟪绦蛟O(shè)計(jì)
- C++從入門到精通(第6版)
- Arduino Wearable Projects
- Struts 2.x權(quán)威指南