Making a variable out of a constant

Started by
12 comments, last by VanillaSnake21 15 years, 4 months ago
[C++] I have a function (that I can't modify), that returns a const value. I need to modify its return value. How can I do it? Thanks.
Advertisement
Assign it to a variable and modify it as you like.
Quote:Original post by asdqwe
[C++] I have a function (that I can't modify), that returns a const value. I need to modify its return value. How can I do it?
Thanks.


It's const for a reason. Why do you think you need to modify it? What do you think "modify" means in this context? The function already returned; you can't cause it to go back in time and return something different. If, for example, you were given a const reference (not a value), then that was done for a reason: the person who wrote the function doesn't want you to modify the referred-to thing via that reference, because it would prevent him from guaranteeing that his code does the things it ought to.

Try showing some actual code, giving some context etc. ?
Quote:Original post by asdqwe
a function [...] that returns a const value

Does that even make sense? Show us the actual function definition.
If I'm understanding you correctly you want to remove the const modifier from the return, if so you could try using const_cast

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Cast it into non-const value.

const int function(...){ return 0;}int other_function(void){    int value = (int)function();    // Since value is not const, even though return value was, you can now modify it.    value += 1;    // It will be returned as non-const as well.    return value;}
Quote:Original post by ville-v
Cast it into non-const value.

And what purpose does your cast serve?
Quote:Original post by Brother Bob
Quote:Original post by ville-v
Cast it into non-const value.

And what purpose does your cast serve?

If you cast const int into normal int, you can place it into normal int instead of const int. It means you can modify the value you have received. That is what the OP asked.
Quote:Original post by VanillaSnake21
If I'm understanding you correctly you want to remove the const modifier from the return, if so you could try using const_cast


No. const_cast is not to make everything mutable, but to provide compatibility to existing C codebases. In fact, the following yields, according to C++-standards, undefined behaviour:

// don't do this:const int i = 3735928559;const_cast <int&> (i) = 42; // <-- undefined behaviour


As long you only read the value of "const_cast <T> (v)" it's okay. But .never. write to it.

[Edited by - phresnel on November 27, 2008 5:35:29 AM]
Quote:Original post by ville-v
Quote:Original post by Brother Bob
Quote:Original post by ville-v
Cast it into non-const value.

And what purpose does your cast serve?

If you cast const int into normal int, you can place it into normal int instead of const int. It means you can modify the value you have received. That is what the OP asked.


No cast is required for this. Casting here is equivalent to reassuring your coworker that making a photocopy of his document doesn't risk introducing any spelling errors into the original. It's self-evident, and C++ requires the compiler to be smart enough to understand this.

This topic is closed to new replies.

Advertisement