Sign in to follow this  
draconar

const whose const is this?

Recommended Posts

I left this piece of code alone so I would think about it at a later time. So this later time came, it is now. :D
class Str{
public:
...
const char& operator[](size_type i) const {return data[i];}
};


Please correct me if I'm wrong, but the first "const char&" will be the return type I'm agreeing to, well, return. but what about the other const keyword right at the end of the operator overload definition? If I'm correct, it is telling the compiler that size_type will be a const iterator, but what about it? should the last const be INSIDE the parameters list? something like (const size_type i){}? hear from you!

Share this post


Link to post
Share on other sites
Quote:
Original post by draconar
what about the other const keyword right at the end of the operator overload definition?


It promises that calling the operator overload (naturally, this works the same way with ordinary member functions) will not change the called-upon Str instance. Thus you are allowed to index into a const Str. This works because you are returning a const reference to the indexed char, so the caller can't use it to change the object.

If you want the calling code to be able to do things like "my_str[42] = 'c'", then you'll need a non-const version that returns a non-const reference to the char. The calling code will, of course, assign to the Str's data via the reference returned by the operator[].

Normally you should provide both. (This is from just a little further down the page that Sneftel linked.)

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman


If you want the calling code to be able to do things like "my_str[42] = 'c'", then you'll need a non-const version that returns a non-const reference to the char. The calling code will, of course, assign to the Str's data via the reference returned by the operator[].



How would the const const version be called by the code? I should have a const reference first in order to assign it?

Share this post


Link to post
Share on other sites
Quote:
Original post by draconar
How would the const const version be called by the code? I should have a const reference first in order to assign it?

AFAIK (i.e. based on experience rather than on specifications) if you have any of these const versions
const char& get() const;
char const& get() const;
char get() const;
and this non-const version
char& get();
and you use it for the purpose of reading like
char myChar = get();
then the compiler will use the const version. If, on the other hand, you use it for the purpose of writing like
get() = myChar;
then the compiler will use the non-const version. Of course, assigning get()'s result to a const pointer will work, too.

Share this post


Link to post
Share on other sites
That would be incorrect. Which version is called depends on what type the member function is being called on. If it's a const pointer or reference to the object the const member function is called. If it's a non-const pointer or reference then the non-const member function is called.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
That would be incorrect. Which version is called depends on what type the member function is being called on. If it's a const pointer or reference to the object the const member function is called. If it's a non-const pointer or reference then the non-const member function is called.
Okay, then my observations have misleaded me.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this