# TUTORIAL: constant (const) type

• genux
• Joined: Jan 22, 2010
• Posts: 106
• Loc: UK
• Status: Offline

January 29th, 2010, 3:59 am

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

C Code: [ Select ]
[
int normalValue = 55;
const int constValue = 10;

1. [
2. int normalValue = 55;
3. const int constValue = 10;
4.

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.
C Code: [ Select ]
const int * pConst= &constValue;

1. const int * pConst= &constValue;
2.

Here is a pointer to a constant variable.
C Code: [ Select ]
int * const pConst2 = &normalValue;

1. int * const pConst2 = &normalValue;
2.

Here is a pointer to a const integer with a constant variable.
C Code: [ Select ]
const int * const pConstConst = &constValue;

1. const int * const pConstConst = &constValue;
2.

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

C Code: [ Select ]
const int returnValue(const int value3) const

1. const int returnValue(const int value3) const
2.

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.

C Code: [ Select ]
#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;
}

1. #include <iostream>
2.
3. using namespace std;
4.
5. class constTest {
6.   private :
7.     int value;
8.   public:
9.     constTest() { value = 0;};
10.
11.     int returnIntConst(int passingValue) const;
12. };
13.
14. // cannot alter any value for the class variables e.g. value in this case.
15. // of course you could do a const_cast.. which takes off the constant (const) type
16. int constTest::returnIntConst(int passingValue) const
17. {
18.     // cannot alter any value inside the function
19.     // assignment of data-member ‘constTest::value’ in read-only structure
20.     // value = 3;
21.     const_cast<int&> (value) = passingValue; // take off the const type restriction
22.     return value;
23. }
24.
25. int returnInt(const int value1)
26. {
27.   // cannot alter the parameter value1
28.   //error: assignment of read-only parameter ‘value1’
29.   // value1 = value1+ 2;
30.   return (value1 + 2);
31. }
32.
33. // returning a constant value, which means that you cannot do anything to the
34. // returning value, but if you assign that value to another variable you can
35. const int returnConstInt(int value1)
36. {
37.     value1 = value1 + 2;
38.     return value1;
39. }
40.
41. int main()
42. {
43.     // cannot alter the return value of the returnConstInt
44.     //  increment of read-only location ‘returnConstInt(3)’
45.     // int constValue = returnConstInt(3)++;
46.     int constValue = returnConstInt(3);
47.     // but you can alter the value of returning int from returnConstInt if passed to a variable
48.     constValue++;
49.     cout << constValue << endl;
50.
51.     constTest cTest;
52.     cout << cTest.returnIntConst(10) << endl;
53.     return 0;
54. }
55.

and the output would be

C Code: [ Select ]
6
10

1. 6
2. 10
3.

## 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.
• Anonymous
• Bot
• Joined: 25 Feb 2008
• Posts: ?
• Loc: Ozzuland
• Status: Online

January 29th, 2010, 3:59 am

## Post Information

• Total Posts in this topic: 1 post
• Moderator: Tutorial Writers
• Users browsing this forum: No registered users and 6 guests
• You cannot post new topics in this forum
• You cannot reply to topics in this forum
• You cannot edit your posts in this forum
• You cannot delete your posts in this forum
• You cannot post attachments in this forum

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.