Properties of Inline Variables

The following are some main properties of Inline Variables:

  • Unlike inline functions, inline variables can have static storage duration. If an inline variable is defined within a class scope, it behaves like a static member variable. Such member variables can be initialized inside the class. It also makes the scope of the variable static.

Example

C++




// C++ program to illustrate the inline static member
// variable
#include <iostream>
using namespace std;
  
// defining class with inline static member variable
class myClass {
public:
    inline static int var = 10;
};
  
// driver code
int main()
{
    // accessing inline static member variable
    cout << myClass::var;
    return 0;
}


Output

10
  • Inline variables have the same memory address across all translation units.
  • C++17 allows multiple definitions of inline variables in different translation units and each translation unit will have its own copy of the variable.
  • Inline variables have external linkage by default. If we want to define an inline variable with internal linkage, we can use a static specifier.

Inline Variables in C++ 17

An inline variable in C++ is a variable that is declared using an inline specifier. It is an exception to one definition having multiple definitions across various translation units. Inline variables have an external linkage when not declared as static.

Similar Reads

Syntax

inline data_type variable_name = initial_value;...

How to Use Inline Variables?

It is very easy to use inline variables in C++. It is just a three-step process to declare and use inline variables in our program:...

Properties of Inline Variables

...

Advantages of Using Inline Variables

...