Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++: const return type

Started by fpsgamer Aug 20, 2006 at 2:36 PM 10 replies 32.1k views
Original Post
fpsgamer
fpsgamer
Im confused about const return types in functions. To me it seems to say that the object that will be returned will be const, that is to say the object will not be modifiable. But I was messing around with it and I am able to modify the const return value. Could someone please explain const function return values.
frob
frob
Turn on strict compiler warnings and errors. You should see either warnings or errors about it.

Make sure you aren't working with a non-const copy of something. Modifying a non-const copy of an object doesn't violate the rules of consts.
Conner McCloud
Conner McCloud
Are you refering to something like this:
const int func(){   return 5;}int main(){   int x = func();   x = 6;}


If so, the issue is that the returned value is const, but it is being copied into a non-const variable. That's perfectly legal...you aren't modifying the original object, so everything's cool.

If not, then provide some code illustrating what you mean. There are several possible interpretations.

CM
fpsgamer
fpsgamer
edit: Conner McCloud showed exactly what I was wondering about ...

But what is the point of returning const if I can store it into a non-const?

Under what circumstances is the constness enforced?
Sr_Guapo
Sr_Guapo
Quote:
Original post by fpsgamer
Quote:
Original post by mikeman
Quote:

But I was messing around with it and I am able to modify the const return value.


How?


*** Source Snippet Removed ***


That is perfectly legal. You return a const value from the function, but then assign the value to a normal variable. That is why you can change it.
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
fpsgamer
fpsgamer
Quote:
Original post by Sr_Guapo
Quote:
Original post by fpsgamer
Quote:
Original post by mikeman
Quote:

But I was messing around with it and I am able to modify the const return value.


How?


*** Source Snippet Removed ***


That is perfectly legal. You return a const value from the function, but then assign the value to a normal variable. That is why you can change it.


Ah, you're quite right. I forgot that you can assign a const to a non-const but not vice versa.

But I'm still bewildered about what the point is of returning a const value.
Is there a situation where the const'ness will be enforced? Otherwise what is the point?
Conner McCloud
Conner McCloud
Quote:
Original post by fpsgamer
edit: Conner McCloud showed exactly what I was wondering about ...

But what is the point of returning const if I can store it into a non-const?

Under what circumstances is the constness enforced?

In that case, there is no point. Returning a const int is a complete waste of a const qualifier.

However, if you return a const reference, its an entirely different story. Now you have access to a const variable directly, so the const keyword provides extra information to both you and the compiler. The following, for instance, should not compile:
const int& func(){   static int x = 42;   return x;}int main(){   func() += 5;}

Even though x itself is not const, you are trying to modify it via a const reference, and you've promised not to do that. THe following is still valid, though:

int main(){   int y = func();   y += 5;   cout << y << "\t" << func();}

That should yield 47 and 42...you are only modifying the copy of x, not x itself.

CM
fpsgamer
fpsgamer
Quote:
Original post by Conner McCloud
Quote:
Original post by fpsgamer
edit: Conner McCloud showed exactly what I was wondering about ...

But what is the point of returning const if I can store it into a non-const?

Under what circumstances is the constness enforced?

In that case, there is no point. Returning a const int is a complete waste of a const qualifier.

However, if you return a const reference, its an entirely different story. Now you have access to a const variable directly, so the const keyword provides extra information to both you and the compiler. The following, for instance, should not compile:

*** Source Snippet Removed ***

That should yield 47 and 42...you are only modifying the copy of x, not x itself.

CM



So it seems like the rule of thumb is that const return values are used when the function name will be used in an expression? Because otherwise the constness has no effect.

edit: also the function has to be returning by reference/pointer
JohnBolton
JohnBolton
Quote:
Original post by fpsgamer
Ah, you're quite right. I forgot that you can assign a const to a non-const but not vice versa.

You can't assign anything to a const object (using an assignment operator) because it is const.

Also, keep in mind that this statement:
    Object const foo = ...; 
uses the copy constructor, not the assignment operator, and the value on the right can usually be const or non-const.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.