<map> and const objects

J

john smith

Hi, I have a question about map and classes that contain maps. My problem
is declaring any class methods const that would do something to the map.
Presumably it's because when operator[] is accessed, the map object can be
changed. Is there any way that I can somehow say that I'm not going to
change the map object?

Also, it becomes a problem when I try to declare the copy constructor like
this:

class A {
A(const A& x); // this has a problem, and has to be A(A& x)
};

any help on this would be appreciated. Thanks.

S.
 
J

john smith

Thanks, I ended up using const_iterators instead.

S
John Harrison said:
john smith said:
Hi, I have a question about map and classes that contain maps. My problem
is declaring any class methods const that would do something to the map.
Presumably it's because when operator[] is accessed, the map object can be
changed. Is there any way that I can somehow say that I'm not going to
change the map object?

Use find instead of operator[]. find cannot alter the map.

john
 

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

No members online now.

Forum statistics

Threads
474,138
Messages
2,570,801
Members
47,347
Latest member
RebekahStu

Latest Threads

Top