C++ Const,Volatile Type Qualifiers

download C++ Const,Volatile Type Qualifiers

of 11

Transcript of C++ Const,Volatile Type Qualifiers

  • 8/13/2019 C++ Const,Volatile Type Qualifiers

    1/11

  • 8/13/2019 C++ Const,Volatile Type Qualifiers

    2/11

    Contents

    Contents

    0.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    0.2 Type Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 0.2.1 Const Type Qualifiers . . . . . . . . . . . . . . . . . . . . . .

    Volatile Type Qualifier . . . . . . . . . . . . . . . . . . . . . .

  • 8/13/2019 C++ Const,Volatile Type Qualifiers

    3/11

    C++ Const,Volatile Type Qualifiers

    C++ Const,Volatile TypeQualifiers

    0.1 Introduction

    This article describes basic concepts of C++ const and volatile typequalifiers.

    0.2 Type Qualifiers

    Type specifiers indicate the type of the object or function beingdeclared.

    Type Qualifiers adds refinement to the type of the object orfunction being declared.

    C++ recognizes const and volatile type qualifiers.

    0.2.1 Const Type Qualifiers

    C const qualifier explicitly declares a data object as somethingthat cannot be changed

    A const object or variable must be initialized and its value is setat initialization.

  • 8/13/2019 C++ Const,Volatile Type Qualifiers

    4/11

    C++ Const,Volatile Type Qualifiers

    In case of class object,all the member variables must be initial-ized in the constructor A default user defined constructor willinitialize all the values to random value.

    When object is defined as constant,no data members of classcan be modified,or any non-constant member function cannot

    be called.

    You cannot use const data objects in expressions requiring amodifiable lvalue,ie left side of assignment

  • 8/13/2019 C++ Const,Volatile Type Qualifiers

    5/11

    C++ Const,Volatile Type Qualifiers

    An object that is declared const is guaranteed to remain con-stant for its lifetime, not throughout the entire execution of

    the program. And thus const variables cannot be used in placeof constant or in a constant expression. The const variable k is

    const for the lifetime of the function,it is initialized to valueof j at the start of the function and remains a const till the

    function returns only.

    A const variable has internal linkage by default,storage qualifiersneed to be specified explicitly. Const Objects can be used inheader files,since they have internal linkages and can be usedinstead of #define for constant values;

    For a const pointer, you must put the keyword between the *and the identifier In this case the the value of pointer/addresscannot be changed but the value the pointer is pointing to canbe changed.

  • 8/13/2019 C++ Const,Volatile Type Qualifiers

    6/11

    C++ Const,Volatile Type Qualifiers

    To define a pointer to a const value,the keyword must be placedbefore the type specifier.

    We can change the values of a,b,c and since in the last linepointer points to address of b,we can deference the pointer

    and obtain the value of new variable.

    we can see that the pointer can be changed,however changingthe value the pointer y points gives a error.It is a pointer to aconst integer

    Any expression of the form

    will give an error.We cannot

    change the value of the variable via pointer.

    In the below expression neither the value pointed to or thepointer can be changed

    Declaring a member function as const means that the thispointer is a pointer to a const object.There are reasons that you will want the compiler to prevent amember function from being able to change any private mem-ber variables of the calling objects

  • 8/13/2019 C++ Const,Volatile Type Qualifiers

    7/11

    C++ Const,Volatile Type Qualifiers

    The data member of class will be constant within that func-tion.

    A const member function cannot call any non-const memberfunction

    If a object of class is delared as const,it can only call constfunctions of the class,but not non-const functions of theclass.Delaring the object as const means that this pointer is

    a pointer to a const object.

  • 8/13/2019 C++ Const,Volatile Type Qualifiers

    8/11

    C++ Const,Volatile Type Qualifiers

    If a function is defined as const we can still change the valueusing const_cast A better approach would be to declare the

    variable desired to be changed as mutable.

    The mutable storage class specifier is used only on a class datamember to make it modifiable even though the member is partof an object declared as const or function is declared as const.

    - Pointer to constant data can be used as function parametersto prevent the function from modifying a parameter passedthrough a pointer or passed as a reference.

  • 8/13/2019 C++ Const,Volatile Type Qualifiers

    9/11

    C++ Const,Volatile Type Qualifiers

    The member function can also return a const value or refer-

    ence to const value. A return of a object invokes a copy con-structor while returning a reference does not invoke a copyconstructor.Thus for efficiency it can be used.However the re-

    turn by reference supplies the local address of a variable,notif the main object has been deallocated,the value which addresspoints to is undefined.The reference should be to an object that exists.

  • 8/13/2019 C++ Const,Volatile Type Qualifiers

    10/11

    C++ Const,Volatile Type Qualifiers

    we can see that value pointed by x4 is difference after the ob-ject has been deallocated. This is inherent drawback of call bereference,where user must ensure that the object is not beingreferenced.

    however in case of applications like streaming camera frames,sensordata etc we do not want to allocate a new address for every

    new frame of data . In this case returning a constant refer-ence or pointer provides a effcient way of providing the datawithout copying it or allocating it everytime.

  • 8/13/2019 C++ Const,Volatile Type Qualifiers

    11/11

    C++ Const,Volatile Type Qualifiers

    Volatile Type Qualifier

    The volatile qualifier maintains consistency of memory access todata objects

    Volatile objects are read from memory each time their value isneeded, and written back to memory each time they are changed.

    The volatile qualifier declares a data object that can have itsvalue changed in ways outside the control or detection of thecompiler and the compiler is thereby notified not to apply certainoptimizations to code referring to the object.

    When applied to a class ,all members of the class have the sametype qualifiers.

    An item can be both volatile and const.In which case the item ismodified by some asynchronous process.

    You can define or declare any function to return a pointer toa volatile or const function.

    You can declare or define a volatile or const function only if itis a nonstatic member function.