Jump to content
  • Advertisement
Sign in to follow this  
nlbs

a const method cant access non-const methods

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

Advertisement
To first order, you are correct. Think of a method as a function that takes an implicit first argument called `this', which is a pointer to the instance on which we are calling the method, and think of a const method as one where the `this' pointer is const. Things should be much more clear then.

Share this post


Link to post
Share on other sites
I've the following Code
const Var& Session::get(const string& key) const{
paramsMap::iterator it = item.find(key);//this line yields error
/*if(it != item.end()){
return item[key];
}*/

const Var& d(0x0);
return d;
}

the above mentioned line yields error.
item is a string (non-const) member variable of the Session Class
paramsMap is a typedef of map<string, Var>
this is the error message shown
error: conversion from
‘std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Var> >’
to non-scalar type
‘std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Var> >’ requested

Share this post


Link to post
Share on other sites
Replace paramsMap::iterator with paramsMap::const_iterator.

Const-correctness rules apply for iterators as well -- iterator is just a kind of pointer (or rather, saying that pointer is a special case of iterator would be more correct). If you have a const object, item, you can't make a non-const iterator to it, because it would allow you to modify the object through that iterator. Const iterators don't allow you modification of the object they point to. And yes, item is const in this case, because .get() is a const function.

Share this post


Link to post
Share on other sites
If I uncomment this Block
the
item[key]
yields error
const Var& Session::get(const string& key) const{
paramsMap::const_iterator it = item.find(key);//this line is ok now
if(it != item.end()){
return item[key];//This line yields error
}
const Var& d(0x0);
return d;
}



But I am not inserting an Item.
error: passing ‘const paramsMap’ as ‘this’ argument of
‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&)
[with
_Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = Var,
_Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >,
_Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
Var> >]’ discards qualifiers

Share this post


Link to post
Share on other sites
//[...]
const Var& d(0x0);
return d;
}


Returning a reference to a local variable, are we? That looks wrong...

Share this post


Link to post
Share on other sites
Quote:
Original post by nlbs
If I uncomment this Block
the
item[key]
yields error
*** Source Snippet Removed ***
But I am not inserting an Item.
error: passing ‘const paramsMap’ as ‘this’ argument of ‘_Tp& ...

You can't use operator[] in a constant map, because operator[] might want to insert a default-constructed value for your key. Just return it->second.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!