difference between front const and end const

Started by
6 comments, last by Zahlman 15 years, 9 months ago
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..
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.

I've updated my post is my comments correct now ??
You're pretty much correct.

See the cpp faq lite on const correctness
[size="1"]
Have a look here at this c++ faq lite section:
[18] Const correctness
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
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 ??
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.
'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.

This topic is closed to new replies.

Advertisement