Jump to content

  • Log In with Google      Sign In   
  • Create Account


Method returning a constant


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Jebbs   Members   -  Reputation: 276

Like
0Likes
Like

Posted 16 September 2012 - 11:03 PM

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!

Sponsor:

#2 Ashaman73   Crossbones+   -  Reputation: 6885

Like
1Likes
Like

Posted 16 September 2012 - 11:39 PM

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...

#3 Álvaro   Crossbones+   -  Reputation: 12389

Like
0Likes
Like

Posted 17 September 2012 - 01:01 AM

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.

#4 L. Spiro   Crossbones+   -  Reputation: 12803

Like
1Likes
Like

Posted 17 September 2012 - 01:07 AM

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, 17 September 2012 - 01:08 AM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#5 Zouflain   Members   -  Reputation: 532

Like
0Likes
Like

Posted 17 September 2012 - 01:26 PM

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, 17 September 2012 - 01:29 PM.


#6 rip-off   Moderators   -  Reputation: 7964

Like
1Likes
Like

Posted 17 September 2012 - 02:54 PM

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.

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS