Jump to content
  • Advertisement
Sign in to follow this  
asdqwe

Making a variable out of a constant

This topic is 3672 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

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

Share this post


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

Share this post


Link to post
Share on other sites
Quote:
Original post by asdqwe
a function [...] that returns a const value

Does that even make sense? Show us the actual function definition.

Share this post


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

Share this post


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

Share this post


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

Share this post


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

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!