Sign in to follow this  
fpsgamer

C++: const return type

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
struct foo
{
void bar() {}
void barfoo() const {}
};

foo foobar1() {return foo();}
const foo foobar2() {return foo();}

int main()
{
foobar1().barfoo(); // perfectly legal, calling const member function of non-const object
foobar1().bar(); // perfectly legal, calling non-const member function of non-const object
foobar2().barfoo(); // perfectly legal, calling const member function of const object
foobar2().bar(); // <

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
er, the last lines should be

foobar2().bar(); // error is here
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this