- 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
推薦閱讀
- Oracle從入門到精通(第3版)
- LaTeX Cookbook
- 看透JavaScript:原理、方法與實(shí)踐
- Apache Spark Graph Processing
- Extending Puppet(Second Edition)
- 現(xiàn)代C++編程實(shí)戰(zhàn):132個(gè)核心技巧示例(原書(shū)第2版)
- Java Web開(kāi)發(fā)實(shí)例大全(基礎(chǔ)卷) (軟件工程師開(kāi)發(fā)大系)
- Android移動(dòng)應(yīng)用項(xiàng)目化教程
- Building UIs with Wijmo
- Python滲透測(cè)試編程技術(shù):方法與實(shí)踐(第2版)
- LabVIEW數(shù)據(jù)采集(第2版)
- 多接入邊緣計(jì)算實(shí)戰(zhàn)
- Java程序設(shè)計(jì)基礎(chǔ)教程
- Hands-On GUI Application Development in Go
- Mastering Rust