# Method returning a constant

This topic is 2164 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys!

I'm fairly new to C++, but when ever I see something I'm unsure about I always try to figure out what's happening. I've seen this a couple of times, but never used it.

[source lang="cpp"]type Foo() const
{
return stuff;
}[/source]

Why is it that the returned value should be a constant? Why wouldn't it be a constant already since in theory you shouldn't be able to alter it?

I feel kind of silly asking this since I am not that new to programming and I feel like this is something I should know, so go easy on me!

##### Share on other sites

Why is it that the returned value should be a constant?

Declaring a method as const means, that this method is not able to modify the object itself. You can see it as 'read-only' declaration. When you got a const object
const MyClass myObject =...
, this object is more or less read-only. You can only call consts methods, which guarantees that this object will not be modified.

Though there're always ways to cheat...

##### Share on other sites
The way I think of methods is the following: A method is a function that takes an implicit first argument --called this'-- which is a pointer to the instance on which it is called. Well, plus some syntactic sugar.

A const method is one in which the this' pointer is const. That's really all there is to it.

##### Share on other sites

I've seen this a couple of times, but never used it.

You should be, when it is appropriate.
The above answer explains what it is but further research can be done by searching for “const correctness”.
In the game companies in which I have worked, it was never required, but it was required at Morgan Stanley and NTT DoCoMo.
In fact it is so important to Morgan Stanley that it is in their interview, and if you don’t give the correct answers, you won’t get the job.
It is just good practice.

[source lang="cpp"]type Foo() const
{
return stuff;
}[/source]

Why is it that the returned value should be a constant? Why wouldn't it be a constant already since in theory you shouldn't be able to alter it?

Your example shows passing a return by value, and making that const is indeed silly.
It is more common when you pass things by reference or by pointer, because the caller could then use those to modify the source object, which is what const is designed to prevent.
If you are returning a reference or a pointer, get into the habit of thinking about whether it should be const or not.

L. Spiro Edited by L. Spiro

##### Share on other sites
 const type Foo(){ return stuff; }
Is VERY different than
 type Foo() const{ return stuff; }
It sounds like you expect the behaviour of the former (that is, to return a "const type"), but what you are seeing means, as was pointed out, "this" is const. It is most useful in cases such as comparitive operator functions (<, ==, >, ect), as it allows you to compare const data types with the assurance that the comparison operators cannot change the values owned by "this" as some dangerous comparison functions might.

For instance, say you're using 2D floating point vectors and you make a Vec2f class. You might have two const Vec2f foo and bar that you need to compare. If you want to perform a foo==bar, the class method Vec2f::operator==(const Vec2f& other){} must also be const (that is, it must read Vec2f::operator==(const Vec2f& other) const{} ) otherwise foo==bar will not compile. The compiler will complain that allowing foo==bar when operator== is not const discards the const attribute of foo. Edited by Zouflain

##### Share on other sites

It is most useful in cases such as comparitive operator functions (<, ==, >, ect), as it allows you to compare const data types with the assurance that the comparison operators cannot change the values owned by "this" as some dangerous comparison functions might.
[/quote]
I don't see why it is more useful in comparison functions than any other function. In general, unless a member function logically needs to modify state it should be marked const so it can be used with values and references/pointers that are declared const.

1. 1
2. 2
JoeJ
20
3. 3
frob
19
4. 4
5. 5

• 10
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632208
• Total Posts
3004774

×