PDA

View Full Version : [SOLVED] C++ const question



t1497f35
August 11th, 2011, 11:58 AM
Hi,
Can anyone please explain what the const is for in this case?:



SomeClass::getSomeValue() const {
return something;
}

dwhitney67
August 11th, 2011, 12:12 PM
It implies (advertises) that the method will not, and cannot, alter any member data of the class when the method is called.

An exception to this rule is when a member data object is declared with the attribute 'mutable'.

t1497f35
August 11th, 2011, 12:15 PM
Thanks a lot!
This "advertising" is simply a protection mechanism or to allow the compiler to make some optimizations? ..or both?

PaulM1985
August 11th, 2011, 12:39 PM
A protection mechanism. I am not aware of any compiler optimizations. It helps for situations like this:



MyObj::Set(int a)
{
m_a = a;
}

int MyObj::Get() const
{
return m_a;
}

void SomeFunc(const MyObj &someObj) // <- someObj is a const reference so you can only call const functions on it
{
int a = someObj.Get(); // <- This line is fine
someObj.Set(a + 1); // <- This line will fail to compile
// because you are trying to call a
// non-const function on a const reference
}


So you know that if you pass in an instance of MyObj into SomeFunc in the example above, your MyObj is safe and the internal state of MyObj will not be meddled with. I know my example is brief and missing lots of code, but hopefully there is enough there for you to get the point, let me know if you need this explaining further.

Paul

vikas.sood
August 11th, 2011, 12:40 PM
To add to it.

If your class object is declared const, only member functions which are const can be called for that object.

eg code


const SomeClass someObj;
someObj.getSomeValue(); // This is OK, since getSomeValue() is const
someObj.callToSomeNonConstFunction() // Compile time error.

t1497f35
August 11th, 2011, 12:50 PM
Thanks anyone, I get it now!

dwhitney67
August 11th, 2011, 01:00 PM
Paul & Vikas,

Both of you made fine comments, however the OP was inquiring about what the attribute 'const' implies when associated at the trailing end of the declaration of a class method.

So, to offer a simple example, here goes:


class Foo
{
public:
...

void setValue(int val)
{
value = val;
valueStale = false;
}

int getValue() const
{
valueStale = true; // ok, because declared mutable
return value;
}

bool isValueStale() const
{
return valueStale;
}

private:
int value;
mutable bool valueStale;
};