[prog] C++: storing objects of different type in a std::map

Riccarda Cassini riccarda.cassini at gmx.de
Thu Sep 23 10:09:28 EST 2004


On Tue, Sep 21, 2004 at 08:27:03AM -0500, Kathryn Hogg wrote:
> 
> It all boils down to pass-by-value vs pass-by-reference.

...exactly, I was under the false impression that the objects I
allocated before the statement 'map["key"] = obj;'  would end up in
the map (i.e. pass by reference), not that they're being duplicated. 
But your very clear and detailed explanation and some playing around
convinced me that the original object is in fact copied to some
seperate storage space allocated for the map - except if I use
pointers.  Good to know, 'cos in my application, copying the objects
wouldn't have achieved the desired effect, anyway, as the original
objects are constantly being modified, after having been inserted in
the map...

I did some more reading (quite a bit, actually), and things already
look a lot clearer now.  Of course, when it comes to C++ in general,
there are still thousands of remaining questions (and with every
questing answered I come up with another one...) but rest assured, I
won't post them all here ;-)
Thanks a bunch for your help, Kathryn!


> I recommend reading a good C++ book.  I'd recommend one but my
> references are a little bit crusty (been programming in C++ for 17
> years)

Funny, that's the typical reaction when you ask experts for
concepts/tutorial-style books or resources :-)  They've been doing
it for so long, so that whatever they were using when they were new,
is either no longer available, or completely outdated (if they can
remember at all).  Beginners on the other hand would often recommend
something which might not be the best around.  Asking people with
intermediate level experience is typically most successful, in my
experience...

Anyhow, if any of you know some good resources, I'd be grateful if you
would send the pointers my way - independently of which expert level
you consider yourself belonging to... (My collegues already recommended
the books by Scott Meyers ("Effective C++, 2nd ed", "More Effective
C++"), which I find to have quite a good effort-to-usefulness ratio.)

Thanks,
Riccarda



More information about the Programming mailing list