vimwiki

Variables

Declaration, assignment and initialization

While similar, there is important distinction between those concept. It apply to class, function, variable…

In the case of variable:

Declaration

[type] [name];

int a;
int b;

Assignment

[name] = [value]

// declaration
int width;

// assignment
width = 5;

Initialization

Declaration + assignment.
Can be complex in C++ The value given to initialized the variable is called initializer

Use this method:

int a { 7 };

Four basics ways:

int a;         // no initializer (default initialization)
int b = 5;     // initializer after equals sign (copy initialization)
int c( 6 );    // initializer in parenthesis (direct initialization)

// List initialization methods (C++11) (preferred)
int d { 7 };   // initializer in braces (direct list initialization)
int e = { 8 }; // initializer in braces after equals sign (copy list initialization)
int f {};      // initializer is empty braces (value initialization)

List initialization using {} are more common and have the benefit to disallow narrowing conversion. (ex: int width{4.5} == error)

Initializing multiple variables

int a = 5, b = 6;          // copy initialization
int c( 7 ), d( 8 );        // direct initialization
int e { 9 }, f { 10 };     // direct brace initialization (preferred)
int g = { 9 }, h = { 10 }; // copy brace initialization
int i {}, j {};            // value initialization

Note: the type is given one time only

Constant variables

Variable whose value cannot be changed after initialization.

Use the const keyword

const double pi = 3.14;

 Constant variable must be initialized (assign a value) when declared.