operator[] for maps

P

pmatos

Hi all,

I'm having some problems with the compilation of the following example
due to a compile time error:
#include <iostream>
#include <map>

using namespace std;

class X {
public:

void insert(const string * id, unsigned int x) { strCodeMap[id] = x;
}
unsigned int getVarCode(const string *id) const { return
strCodeMap[id]; }

private:

struct ltstr {
bool operator()(const string * s1, const string * s2) const {
return *s1 < *s2;
}
};

map<const string *, unsigned int, ltstr> strCodeMap;
};

int main() {

X c;
const string * s1 = new string("Hello");
const string * s2 = new string("Buh");

c.insert(s1, 3);
c.insert(s2, 4);

const string * s4 = new string("Hello");

cout << "S4 VALUE: " << c.getVarCode(s4) << "\n";

delete s1;
delete s2;
delete s4;

return 0;
}

The compile time error is in the public method of class X:
unsigned int getVarCode(const string *id) const { return
strCodeMap[id]; }

And is:
$ g++ maptest.cc -o maptest
maptest.cc: In member function `unsigned int X::getVarCode(const
std::string*)
const':
maptest.cc:10: error: passing `const std::map<const std::string*,
unsigned int,
X::ltstr, std::allocator<std::pair<const std::string* const,
unsigned int> >
>' as `this' argument of `_Tp& std::map<_Key, _Tp, _Compare,
_Alloc>::eek:perator[](const _Key&) [with _Key = const std::string*,
_Tp =
unsigned int, _Compare = X::ltstr, _Alloc =
std::allocator<std::pair<const
std::string* const, unsigned int> >]' discards qualifiers

Can someone give me a hint on what this means?

Cheers,

Paulo Matos
 
A

Andre Kostur

Hi all,

I'm having some problems with the compilation of the following example
due to a compile time error:
#include <iostream>
#include <map>

using namespace std;

class X {
public:

void insert(const string * id, unsigned int x) { strCodeMap[id] = x;
}
unsigned int getVarCode(const string *id) const { return
strCodeMap[id]; }

operator[] is a non-const method of std::map. So you can't call it from
a const method in your class (since strCodeMap is a member of your
class). Food for thought: what does getVarCode return if id doesn't
exist in the map?
 
V

Victor Bazarov

pmatos said:
I'm having some problems with the compilation of the following example
due to a compile time error:
#include <iostream>
#include <map>

using namespace std;

class X {
public:

void insert(const string * id, unsigned int x) { strCodeMap[id] = x;
}
unsigned int getVarCode(const string *id) const { return
strCodeMap[id]; }

You cannot use [] on a const map. This member function is 'const', so
all the members are const.

V
 
E

Earl Purple

Actually normally if you call operator[] on a map for an element that
doesn't exist, it creates the element with a default value and gives
you a reference to it. It can be a very useful way to look up and
element and insert if it doesn't exist (for example, a cache / lazy
evaluation).

You cannot use it for const lookup. Instead you should use an iterator
and the method find().
Of course you can define a const overload for operator[] in your own
class if you like.

The reasons they didn't make a const overload for operator[] in map
are:
- If the element doesn't exist, it cannot create one, so what would it
return you a reference to?
- If you were allowed to use this for reading, and you actually had a
non-const map it would have the side-effect of inserting an element
when you didn't want to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top