constness can be considered as addional level of type information and therefore we can overload methods in C++ based on only const properties. const-ness of a function should capture the abstract state of the object and not the physical bit state. Following class has 2 overloaded methods which differ only in the const-ness. Remember, subscript operators, if you need one you need the other.
class Fred { ... };
class MyFredList {
public:
const Fred& operator[] (unsigned index) const; // first
Fred& operator[] (unsigned index); // second
...
};
A const object invokes first method therefore after returning the reference to internal data structure, you can not modify as it is const. A non const object invokes the second memeber function in which you can indeed modify returned Fred object. While returning references to internal data structure either return a const reference or return by value if you don't want it to be modified.
An exhaustive information on const-correctness is here:
http://www.parashift.com/c++-faq-lite/const-correctness.html
class Fred { ... };
class MyFredList {
public:
const Fred& operator[] (unsigned index) const; // first
Fred& operator[] (unsigned index); // second
...
};
A const object invokes first method therefore after returning the reference to internal data structure, you can not modify as it is const. A non const object invokes the second memeber function in which you can indeed modify returned Fred object. While returning references to internal data structure either return a const reference or return by value if you don't want it to be modified.
An exhaustive information on const-correctness is here:
http://www.parashift.com/c++-faq-lite/const-correctness.html
Comments
this site again. I just worship the site its comes
from! Believe me in my extra time I'm consistently
looking up blogs like this.
Please examine my 1800contacts coupon blog as soon as you can.
and we had to go back to it! Every chance I get on the
net I search for blogs just like your site.
Everyday of the month you need to peep my 1800contacts web coupon code blog.
site again.I'm consistently looking up blogs like
this.
Please take a journey to my 1800contacts web coupon code blog.
will return! When I get the time I look for blog like
this one.
In my spare time I will look for your blog.
back! I love surfing the internet for blogs that are
exactly like this blog.
Please go over my plastic surgery houston blog.
will go back! Surfing online for blogs like this one
is worth my time.
Please examine my plastic surgery boston blog as soon as you can.