O'Reilly logo

Mastering C++ Programming by Jeganathan Swaminathan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required