Home C++ Introduction Decisions Loops Input/Output Functions Stack and Heap References Arrays Searching and Sorting Recursion Pointers Character and Strings Structures Classes Inheritance Exceptions Templatess STL Modern C++ Misc Books ----

Classes


Contents


The word const can be used in function arguments. This means the object cannot be modified. We can call the methods of the object if they have been declared as "const" .
File: const1.cpp
// C++ program to demonstrate the
// constant function
#include <iostream>
using namespace std;

// Class Test
class Test {
    int value;

public:
    // Constructor
    Test(int v = 0)
    {
        value = v;
    }

    // We get compiler error if we
    // add a line like "value = 100;"
    // in this function.
    int getValue() const
    {
        return value;
    }
    
    // a nonconst function trying to modify value
    void setValue(int val) {
        value = val;
    }
};

// Driver Code
int main()
{
    // Object of the class T
    Test t(20);

    // non-const object invoking const function, no error
    cout << t.getValue() << endl;
    
    // const object
    const Test t_const(10);

    // const object invoking const function, no error
    cout << t_const.getValue() << endl;

    // const object invoking non-const function, CTE
    // t_const.setValue(15);
    
    // non-const object invoking non-const function, no error
    t.setValue(12);
    
    cout << t.getValue() << endl;

    return 0;
}
Same rules apply if we have a constant object in the argument to a function.
File: const2.cpp
// C++ program to demonstrate the
// constant function
#include <iostream>
using namespace std;

// Class Test
class Test {
    int value;

public:
    // Constructor
    Test(int v = 0)
    {
        value = v;
    }

    // We get compiler error if we
    // add a line like "value = 100;"
    // in this function.
    int getValue() const
    {
        return value;
    }

    // a nonconst function trying to modify value
    void setValue(int val) {
        value = val;
    }
};


void function(const Test& constObject  )
{
    //allowed
   constObject.getValue() ;

   //Compiler error
   //constObject.setValue(12) ;
}

// Driver Code
int main()
{
    // Object of the class T
    Test t(20);

    // non-const object invoking const function, no error
    cout << t.getValue() << endl;

    // const object
    const Test t_const(10);

    // const object invoking const function, no error
    cout << t_const.getValue() << endl;

    // const object invoking non-const function, CTE
    // t_const.setValue(15);

    // non-const object invoking non-const function, no error
    t.setValue(12);

    cout << t.getValue() << endl;

    return 0;
}
In certain cases we may want the constant function to be able to modify certain variables and we can use the word mutable for that.
File: const3.cpp
// C++ program to demonstrate the
// constant function
#include <iostream>
using namespace std;

// Class Test
class Test
{
    int value;
    mutable int x1 ;
public:
    // Constructor
    Test(int v = 0)
    {
        value = v;
    }

    // We get compiler error if we
    // add a line like "value = 100;"
    // in this function.
    int getValue() const
    {
        return value;
    }

    // a nonconst function trying to modify value
    void setValue(int val)
    {
        value = val;
    }

    void changeValueX( int p1 ) const
    {
       //if x1 is not mutable then compiler error
       x1 = p1  ;
    }



};


void function(const Test& constObject  )
{
    //allowed
   constObject.getValue() ;

   //Compiler error
   //constObject.setValue(12) ;
}

// Driver Code
int main()
{
    // Object of the class T
    Test t(20);

    // non-const object invoking const function, no error
    cout << t.getValue() << endl;

    // const object
    const Test t_const(10);

    // const object invoking const function, no error
    cout << t_const.getValue() << endl;

    // const object invoking non-const function, CTE
    // t_const.setValue(15);

    // non-const object invoking non-const function, no error
    t.setValue(12);

    cout << t.getValue() << endl;

    return 0;
}

Friend

Const functions

OO

Smart Pointer