Re: Is it legal to use maps with refrences ?
On Jun 25, 2:50 am, Timothy Madden <terminato...@gmail.com> wrote:
Hello
My compiler complains if I try to declare
std::map<unsigned, RECT const &>::iterator it
in the argument list for a function declaration.
So I would like to know if it is legal to declare such a container.
I don't think so. The problem is, that would produce "references to
references" in quite a bit of places.
So pointers are in order. You might want to wrap the pointer in a
reference-like object and use that in your map ?
( NB: very limited usability; compiled with head-compiler and tested
with head-debugger ;-) )
class rect_holder
{
public:
rect_holder(const RECT& p) :_p(&p) {}
operator const RECT&() const { assert(p); return *p; }
private:
const RECT* _p;
};
std::map<unsigned, const rect_holder> mmm;
rect_holder gives you "copyability", and you can even do e.g.
const RECT& r = your_map[1];
But note also: with a map, if your value type is not const, you can't
use operator[] like so:
your_map[key] = value;
So IMO it's not a good idea to use const value type. You should
instead pass const map around.
Goran.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]