Jump to content
  • Advertisement
Sign in to follow this  
nlbs

difference between front const and end const

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

Whats the difference between
const void X::y(){
  //I think it assure's that even if it returns a pointer or a refrence it cant be changed
  //such that the pointee or referenced item will be changed.
  //Plese Correct me if I am wrong.
}

and
void X::y() const{
  //Does not alter any data of the class
    //so can't call any non-const member function too
  //Plese Correct me if I am wrong.
}

Edit: forgot to add a return Type. So now is my Comments Correct ?? I think Wrong..

Share this post


Link to post
Share on other sites
Advertisement
Both snippets are incorrect, since your functions lack a return type. However, had you added that return type, then both interpretations would have been correct.

Share this post


Link to post
Share on other sites
so in case of
const void X::y();

1. If my function body doesn't return a const variable would it be automatically const_cast ed ??
2. Is it necessary to const_cast the variable that is going to be returned.
3. what does it means if I do return 2; in a const int X::y(); since I am not returning any variable.
>> I think it doesnt make any sence right ??

Share this post


Link to post
Share on other sites
Quote:
Original post by nlbs
1. If my function body doesn't return a const variable would it be automatically const_cast ed ??
2. Is it necessary to const_cast the variable that is going to be returned.
3. what does it means if I do return 2; in a const int X::y(); since I am not returning any variable.

1) Sort of, it gets implicitly converted to const.
2) Nope.
3) You're returning a constant expression, which is fine.

Share this post


Link to post
Share on other sites
'const void' is nonsensical. 'const int X::y()' means that the calling code can't change the int that is returned.

Normally, 'const' on the return type is only useful for class/struct types, or for returns by reference (remember that you can't return a reference to a local variable; usually you would be returning a reference to a member instead.)

const_cast is used to remove const. It's a dangerous tool. Adding const to something is automatic where it's needed.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!