Constants must be initialised at the point of instantiation.
Global constant (internal linkage):
// file: constants.h
const int x = 42;
Every file that includes constants.h gets its own discrete copy of x which the compiler may be able to optimise away (no guarantee). However, internal linkage allows integral constants to be used as integral constant expressions.
Global constant (external linkage):
// file: constants.h
extern const int x;
// file: constants.cpp
external const int x = 42;
External linkage prohibits the use of integral constant expressions other than in the unit that defines the integral. However, for non-integral expressions, external linkage ensures each translation unit uses the same instance of the global, rather than a discrete copy, thus reducing memory consumption.
Ideally, global constants should be placed in a separate namespace to avoid polluting the global namespace:
// file: constants.h
namespace constants { const int x = 42; };
This header can be included in any translation unit that requires it:
// file: source.cpp
#include "constants.h"
int value = constants::x;
Function local constants must be initialised when they are declared:
void foo()
{
const int x = 42;
// ...
}
Function argument constants are initialised when the function is invoked:
void foo(const int x)
{
}
void bar()
{
foo (40+2); // assign the constant expression 42 to the constant x argument in foo
}
Static member constants must be initialised outside of the class declaration (preferably in the class implementation file):
// file: foo.h
class foo
{
static const int x;
};
// file: foo.cpp
const int foo::x = 42;
Nonstatic member constants must be initialised via the class constructor initialisation lists:
class foo
{
const int x;
foo (const int _x = 0): x{_x} {} // default constructor
foo (const foo& source): x{source.x} {} // copy constructor
foo (foo&& source): x{source.x} {} // move constructor (cannot move a constant)
};
Note that objects with constant member variables cannot use copy assign or move assign operators to reassign their constant members. Once assigned, a constant cannot be changed, even with a const_cast.
Constants must be initialised at the point of instantiation. Initialisation can be done in one of three ways:
const char c = 'A';
const char c ('A');
const char c {'A'};
All three methods are essentially the same when applied to primitives like int or char. For more complex objects you will usually use the second method to invoke a specific constructor. The last method performs a member-wise initialisation, but is only supported by classes that include a constructor that explicitly accepts an initialisation list as an argument. Ideally, every class you write should employ an initialisation list and every initialisation should use this method because it makes it clear to the reader that an initialisation is taking place.
Is an important thing to do.
The general order of initialization is:Base class objects (if present)Member data objectsConstructor function code
Instantiation is creating the instance of the variable/object . While Initialization is to provide the variable with some value. int i; // i is an instance of an integer i=10; //initialised with the value 10
Initialization. Hint: your compiler might warn you, neverignore warnings if you aren't absolutely sure what they mean.
Definition. Example: extern int x1; /* declaration */ int x2; /* definition */ int x3= 2; /* definition with initialization */
Is an important thing to do.
The general order of initialization is:Base class objects (if present)Member data objectsConstructor function code
Not initialized variable: int myInt; Initialized variable: int myInt = 10;
Instantiation is creating the instance of the variable/object . While Initialization is to provide the variable with some value. int i; // i is an instance of an integer i=10; //initialised with the value 10
Constant data and constant functions.
A constant is a variable that does not change. The correct term is constant variable.
Initialization. Hint: your compiler might warn you, neverignore warnings if you aren't absolutely sure what they mean.
Initialization of objects means to provide an initial value for the object. This is usually done by the constructor, or it can be done with an assignment statement.
A constant object is one that, once initialized, never changes value.
c is any constant value
Initialization is nothing but assigning some value to a parameter. ex :- int a; // Defination of an integer variable a = 3; // Initialization of the variable a
Pass by value, constant value, reference and constant reference. Pass by value is the default in C++ (pass by reference is the default in Java).