Hello guys.. i am really new to C++. I feel a bit confused with the using of const in this code.

class B {
        private:
            int x;
        public int func() const { return x; }
    }

Viewing at that source code,, what is really functions of const ??

For your appreciation, I say thanks.

const just says the function will not change anything

oh i see.. so what's really the aim of using const ? To protect its object from anything ? and how to call that function using const ?

In the case of that function just call it like any other function that does not have the const keyword. The const keyword does nothing more than tell you that the function does not change anything. The following code will produce an error

class A
{
public:
    A() {x = 1;}
    int getx() const 
    {
        x = 2;  // <<< ERROR HERE
        return x;
    }
private:
    int x;
};

int main() 
{ 
    A a;
    int k = a.getx();
   return 0;
    
}

Edited 7 Years Ago by Ancient Dragon: n/a

const just says the function will not change anything

Kind of. const can only be applied to methods, and it means two things:

  1. The immutable state of the object will not be changed within a const method.
  2. The const method can be called by objects that are declared as const.

The mutable keyword was introduced to allow mutable data for const objects. The idea is that mutable data does not contribute to the constness state of the object. Mutable fields can be modified in a const method. Here is a sample program that displays the concept of const methods and mutable fields:

class Test
{
    int _x;
    mutable int _y;
public:
    Test(int x, int y): _x(x), _y(y) {}

    void Method1()
    {
        _x = 0; // OK, _x is mutable in a non-const method
        _y = 0; // OK, _y is mutable in all cases
    }

    void Method2() const
    {
        _x = 0; // will not compile, _x is immmutable in a const method
        _y = 0; // OK, _y is mutable in all cases
    }
};

int main()
{
    Test a(1, 2);
    Test const b(1, 2);

    a.Method1(); // OK, non-const object calling non-const method
    a.Method2(); // OK, non-const object calling const method
    b.Method1(); // will not compile, b is const, but Method1() is not
    b.Method2(); // OK, b is const and Method2() is const
}

Edited 7 Years Ago by Tom Gunn: n/a

In the case of that function just call it like any other function that does not have the const keyword. The const keyword does nothing more than tell you that the function does not change anything. The following code will produce an error

class A
{
public:
    A() {x = 1;}
    int getx() const 
    {
        x = 2;  // <<< ERROR HERE
        return x;
    }
private:
    int x;
};

int main() 
{ 
    A a;
    int k = a.getx();
   return 0;
    
}

um..
consider if in the main program, after the statement int k = a.getx();
we add one statements like cout <<k; what will that cout syntax print ? is 2 or 1 ?

um..
consider if in the main program, after the statement int k = a.getx();
we add one statements like cout <<k; what will that cout syntax print ? is 2 or 1 ?

The program will not execute because the compiler will not create the program. You will have to fix the error.

This article has been dead for over six months. Start a new discussion instead.