a const method cant access non-const methods

Started by
28 comments, last by Driv3MeFar 15 years, 8 months ago
in C++ a const method can not access non-const methods am I Right ?
Advertisement
Not on the same object, no.
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.
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
try using paramsMap::const_iterator
OOPS Silly mistake I need to use const_iterator instead of iterator.
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.
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
//[...]	const Var& d(0x0);	return d;}

Returning a reference to a local variable, are we? That looks wrong...
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.

This topic is closed to new replies.

Advertisement