Broad Network


The Specifier const in front and behind a C++ Function Prototype

Some Features of C++ Entities – Part 11

Forward: In this part of the series, I talk about the effect of placing the specifier, const, in front or behind a C++ function prototype.

By: Chrysanthus Date Published: 25 Sep 2012

Introduction

This is part 11 of my series, Some Features of C++ Entities. In this part of the series, I talk about the effect of placing the specifier, const, in front or behind a C++ function prototype. I will use function definitions (descriptions) for the explannations. After that, to have a prototype, just type the first line of the function definition ending it with a semicolon. The use of the function definition is the same use of the function prototype. The function definition simply has the function body. You should be reading this series in the order given.

The const in front

Ordinary Return Type with const
Consider the following program with an ordinary return type for a function with const:

#include <iostream>
using namespace std;

    const int fn()
        {
            int ident = 5;
            
            return ident;
        }

    int main()
        {
            int mIdent = fn();
    
            cout << mIdent;
    
         return 0;
        }

The prototype for the above function is:

    const int fn();

In this case, it does not matter whether you return a constant value, as long as you return a type in your return statement that is the same as the function return type (int in this case).

Constant Pointed Object Return Type
In this case, the return type of the function is indicating that the value of a pointed object is constant. Consider the function in the following program:

#include <iostream>
using namespace std;

    const int* fn()
        {
            int myInt;
            int* ident = &myInt;
            *ident = 5;
            
            return ident;
        }

    int main()
        {
            const int* mIdent = fn();
    
            cout << *mIdent;
    
         return 0;
        }

In this case, you have a normal pointer declared inside the function body. The pointer is returned in the same way that it would be returned if const did not exist in front of the function in the return type.

The const of the return type indicates that the value of the pointed object in the function body is const. In main the return pointer (address) has to be received by a constant pointed object declaration. In the above program, the pointers, ident and mIdent have the same address (they both point to the same constant value object).

The prototype for the above function is:

    const int* fn();

Constant Pointer to Pointed Object Return Type
In this case, the return type of the function is indicating that the pointer (address) of a pointed object and not the value of the pointed object is constant. Consider the function in the following program:

#include <iostream>
using namespace std;

     int *const fn()
        {
            int myInt;
            int* ident = &myInt;
            *ident = 5;
            
            return ident;
        }

    int main()
        {
            int *const mIdent = fn();
    
            cout << *mIdent;
    
         return 0;
        }

In this case, you have a normal pointer declared inside the function body. The pointer is returned in the same way that it would be returned if const did not exist in front of the function in the return type.

The const of the return type indicates that the address (value) of the pointer in the function body is const. In main the return pointer (address) has to be received by a constant pointer declaration. In the above program, the pointers, ident and mIdent have the same address (they both point to the same object).

The prototype for the above function is:

     int *const fn();

Constant Referenced Value Return Type
In this case, the return type of the function is indicating that the referenced value of an object is constant. Consider the function in the following program:

#include <iostream>
using namespace std;

    const int& fn()
        {
            int hisInt = 5;
            int &herInt = hisInt;
            
            return herInt;
        }

    int main()
        {
            int ident = fn();
    
            cout << ident;
    
         return 0;
        }

In this case, you have a normal reference declared inside the function body. The reference identifier value is returned in the same way that it would be returned if const did not exist in front of the function in the return type.

The const of the return type indicates that the value of the reference identifier in the function body is const through the reference identifier. In main the return value can be received by a non-constant or constant ordinary identifier.

The prototype for the above function is:

    const int& fn();

The const Behind

The const at end of Prototype
In the case where const is behind (at end of) the prototype, such a function is a member function of a class. That is, such a function is part of a class. That is, such a function is a method of a class. With the const specifier at the end, the function can only use (read) the value of a data member (property) of the same class, but cannot change it in its body (block).

So, in a class, you can forbid a method definition from changing the value of data members using the word, const. Note that in this case, the member function and data members belong to the same class (object).

The following code will not compile because the member function definition tries to change (or give) the value of the data member (property):

#include <iostream>
using namespace std;

class CC
    {
        int prop1;

        void mthd() const
            {
                prop1 = 3;
            }
    };

int main()
    {

        return 0;
    }

Note the position of the word, const, in the member function (method) declaration. The word is on the right of the signature. By this position, the function block cannot change the value of the data member. The prototype of the member function is:

        void CC::mthd() const;

Also, the function body with const at the end of its prototype, cannot change the data member of a class to which it is a friend. The function body can however, change the value of a data member or identifier outside its class and outside the class to which it is a friend.

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