answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

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.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Initialization of a constant in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Initialization of variables in namespace in C plus plus?

Is an important thing to do.


What is the order of initialization of data in C plus plus?

The general order of initialization is:Base class objects (if present)Member data objectsConstructor function code


What do you mean by initialization in c plus plus?

Not initialized variable: int myInt; Initialized variable: int myInt = 10;


What is the difference between instantiation and initialization in C plus plus?

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


What are the two types of constant in c plus plus?

Constant data and constant functions.


What does a constant do in c plus plus?

A constant is a variable that does not change. The correct term is constant variable.


How do you avoid garbage values in c plus plus programs?

Initialization. Hint: your compiler might warn you, neverignore warnings if you aren't absolutely sure what they mean.


What do you mean by initialisation of objects in c plus plus?

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.


What is a constant object in c plus plus?

A constant object is one that, once initialized, never changes value.


What does c mean in y equals mx plus c?

c is any constant value


What is the intialization in c?

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


What are the different parameter passing methods used by c plus plus?

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).