Saturday, October 08, 2005

const-correctness

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

7 comments:

dat-girl said...

Unique blog my friend, I can hardly wait to vist
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.

outa-time said...

Creative blog.  I thought it was the best site out
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.

maryland mortgagee said...

Unbelievable blog. I can hardly wait to vist this
site again.I'm consistently looking up blogs like
this.
Please take a journey to my 1800contacts web coupon code blog.

cash advance dallas said...

Charming blog. Your site was off the chain and I
will return! When I get the time I look for blog like
this one.
In my spare time I will look for your blog.

job opportunitya said...

Exciting blog. The site out did itself and will be
back! I love surfing the internet for blogs that are
exactly like this blog.
Please go over my plastic surgery houston blog.

job opportunitya said...

Sensational blog. I took pleasure in the site and I
will go back! Surfing online for blogs like this one
is worth my time.
Sweetie, go and search my plastic surgery michigan blog for what you need.

job opportunitya said...

Sensational blog. I took pleasure in the site and I
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.