Jump to content
  • Advertisement
Sign in to follow this  
jdehaan

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!