Broad Network


C++ cv-qualifiers

Specifiers in C++ - Part 4

Forward: In this part of the series I explain what is called, cv-qualifiers.

By: Chrysanthus Date Published: 25 Aug 2012

Introduction

This is part 4 of my series, Specifiers in C++. I assume you have been reading the tutorial in the order given; because this is a continuation. In this part of the series I explain what is called, cv-qualifiers.

The specifier, const, in C++, is so important, that it is treated specially. The specifier, volatile, is like the opposite of, const. It is optional to type and that is what you have been doing in coding, already. So, cv-qualifiers are “const” and “volatile”.

The volatile Example
Consider the following program:

#include <iostream>
using namespace std;

volatile int myInt = 30;

int main()
    {
        myInt = 40;

        cout << myInt;

        return 0;
    }

In the above program, myInt, is volatile. So its value can be changed. That is, the value identified by myInt can be changed. In main, the value is changed from 30 to 40.

The identifier, myInt, as declared, is a global identifier, so it can be seen and used in a function (local) scope, like that of main. However, scoping is not the topic of interest here. That was just a recall, so that you do not get confused with what was studied in the previous part of the series.

The volatile specifier is optional and it is always omitted. So, the above code is the same as the one below:

#include <iostream>
using namespace std;

int myInt = 30;

int main()
    {
        myInt = 40;

        cout << myInt;

        return 0;
    }

Note that the volatile specifier has not been used, in this program.

The const Specifier
The const specifier, makes something constant. It can make the value in a region in memory constant and it can make the address pointing to the region in memory, constant.

Consider the following program:

#include <iostream>
using namespace std;

const int myInt = 30;

int main()
    {

        cout << myInt;

        return 0;
    }

In this program, the value identified by the identifier, myInt, is always constant and cannot be changed. It cannot be changed, as the volatile case was change in a previous code above.

The const Specifier and Pointers
When dealing with pointers, there are 2 objects: the pointer object and the pointed object. You can always choose to make either of them constant. To make the value of the pointed object constant, you would declare something like:

    const int *myPointr

Under this condition you cannot change the value of the pointed object using the pointer. However, you can change the value through some other means.

To make the value (address) of the pointer object constant, you would declare something like:

    int *const myPointr

Note the positions of the specifier, const in the two declarations. Also note the position of the asterisk in the two declarations.

The const Specifier and the Reference
In the following statement, the address (&herInt) is kept constant:

          const int &herInt = hisInt;

Initialization for Constant Objects
The following declarations with the const specifier must be initialized (these have been initialized).

    const int myInt = 30;

    int *const myPointer = &hisInt;

    const int &herInt = hisInt;

The following declaration with the const specifier does not need initialization:

    const int *myInt;

This is the problem with C++. C++ has a lot of interwoven features. So this is the rule I deduce from the above: Except for constant value (pointed object content) from a pointer object, in a declaration with the const specifier, there must be initialization.

The const specifier at end of Function Declaration
You might have seen something like:

        void functionName(int varA, float varB) const;

This declaration is correct. Here the specifier, const is at the end of the function declaration. You will normally find this kind of declaration as a member function (method) of a class. It means that the member function cannot change the value of any data member (property) of the class.

Function Signature
The function signature is the pair of brackets and its content of a function interface (declaration). However, when you have the const specifier at the end of a function declaration (interface) the specifier is also part of the function signature. So if you have two function interfaces (declarations) that differ only with the presence or absence of this const specifier at the end, then that is overloading.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message