Introduction

A const (constant) type means that the variable(s) are of constant type and cannot be altered in there present state. You can const_cast them to take off the const type and have done it within the class example.

Constant variables

There are couple of places that you can put the const type for a variable.

[
int normalValue = 55;
const int constValue = 10;

The normal value is 55 and can be altered, but the constValue is 10 and cannot be altered, it is good for a value of pi for example.

Pointers constant variables

Pointers to can be constant types, because they can point to a constant variable and also have there pointed to value a constant value so that they always point to the same variable.

A pointer to a constant int variable, the pointer value can be changed to point to another const int variable.

const int * pConst= &constValue;

Here is a pointer to a constant variable.

int * const pConst2 = &normalValue;

Here is a pointer to a const integer with a constant variable.

const int * const pConstConst = &constValue;

The constant pointer cannot be altered and also the pConstConst memory location that is pointed to e.g. constValue cannot change either.

Functions with constants

Functions can also have const types attached to them, there are three different places

const int returnValue(const int value3) const

In order of the constants (const) within the function definition

  1. The return const int cannot be altered, you can alter the value if you place the return into a different variable
  2. The parameter value cannot be altered.
  3. The variables within the "this" class cannot be altered.

Below is a example of the above, I have included the error messages if you try to compile up the program when you violating the const type.

#include <iostream>

using namespace std;

class constTest {
  private : 
    int value;
  public:
    constTest() { value = 0;};

    int returnIntConst(int passingValue) const;
};

// cannot alter any value for the class variables e.g. value in this case.
// of course you could do a const_cast.. which takes off the constant (const) type
int constTest::returnIntConst(int passingValue) const
{
    // cannot alter any value inside the function
    // assignment of data-member 'constTest::value' in read-only structure
    // value = 3;
    const_cast<int&> (value) = passingValue;	// take off the const type restriction
    return value;
}

int returnInt(const int value1)
{
  // cannot alter the parameter value1
  //error: assignment of read-only parameter 'value1'
  // value1 = value1+ 2;
  return (value1 + 2);
}

// returning a constant value, which means that you cannot do anything to the 
// returning value, but if you assign that value to another variable you can
const int returnConstInt(int value1)
{
    value1 = value1 + 2;
    return value1;
}

int main()
{
    // cannot alter the return value of the returnConstInt 
    //  increment of read-only location 'returnConstInt(3)'
    // int constValue = returnConstInt(3)++;
    int constValue = returnConstInt(3);
    // but you can alter the value of returning int from returnConstInt if passed to a variable
    constValue++;
    cout << constValue << endl;

    constTest cTest;
    cout << cTest.returnIntConst(10) << endl;
    return 0;
}

and the output would be

6
10

Conclusion

The constant (const) type restricts variables from being altered if you do not want them to be, saves on any potential problems if you are trying to alter a variable that needs to be the same, e.g. pi .. it should not alter from 3.142 to 5.

I do really like to have any feedback regarding any tutorial/post, just reply or PM me.. glad to help, better to share knowledge.

This page was published on It was last revised on

0

0 Comments

  • Votes
  • Oldest
  • Latest