Re: std::map question

From: David Hilsee (davidhilseenews_at_yahoo.com)
Date: 08/14/04


Date: Sat, 14 Aug 2004 10:45:24 -0400


"Flzw" <flownz@wanadoo.fr> wrote in message
news:cfl6v9$5pj$1@news-reader5.wanadoo.fr...
> Well I have a map like this :
>
> std::map <string, CObject> ObjectList;
>
> I have a function like this :
>
> CObject* NewObject( char* Name, CArg* Arg)
> {
> std::string key = Name;
> ObjectList[ key] = CObject( Name, Arg);
> return &ObjectList[Name];
> }
>
> I tried to compile and it complained that there was no default constructor
> for CObject
>
> So I changed my constructor declaration to :
> CObject::CObject( char* Name = "Default", CArg* Arg = NULL);
>
> Just to sse because I can't create it without a valid CArg pointer.
>
> it compiles when I call NewObject it crashes, with the debugger I saw it
> seemed to call the constructor with the correct arguments first, and then
> call it again with no arguments (which in this case makes my program
crash),
> but I don't understant why.
>
> If someon ecould explain what I don't understand here....

I don't understand the behavior you described, but I'll explain what the
behavior should be.

ObjectList[key] = CObject(Name, Arg);

The line above creates a default-constructed CObject and attempts to insert
it into the std::map as the value mapped to the key. If there is already a
value mapped to the key, then the std::map is left unchanged. The
std::map::operator[] then returns a reference to the CObject instance mapped
to key (could be the default-constructed object I just mentioned or the one
that was already present). Then, CObject's assignment operator is invoked,
effectively replacing the contents of the instance stored inside the
std::map.

If you don't want to have to specify a default constructor, you can use
std::map::insert instead of operator[]. That function returns an iterator
and a boolean, so you can check to see if the insert did not insert a new
key-value pair into the map and then use the iterator to modify the existing
value in that case.

> An other quick question, does &ObjectList[ Name] return NULL if Name does
> not match a key in the map ?

No, in that case, operator[] inserts a new key-value pair into the map and
you get a pointer to the default-constructed CObject instance.

-- 
David Hilsee


Relevant Pages

  • Re: Use the accessor! Was: Copy Constructor Usage
    ... I think you are getting too hung up on the copy constructor bit. ... accessor fns on some bogus efficiency argument. ... a field 'leng' might map onto an accessor 'size'; ... No style or design rules give us a free lunch. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: confusing delete problem
    ... Joerg Toellner wrote: ... > thx for your replies. ... > Can't understand your comments about copy constructor. ... > definitely ONE instance/object of type class map. ...
    (comp.lang.cpp)
  • Re: confusing delete problem
    ... Of course x is defined in the real code destructor. ... Can't understand your comments about copy constructor. ... definitely ONE instance/object of type class map. ...
    (comp.lang.cpp)
  • Re: Get list of unique words in a string
    ... may declare/define the same identifiers. ... additional "namespace" for all libraries of PointedEars' JavaScript ... Why do you declare functions in Map which don't use any of map ... _Value.isInstance and few others could all be taken out of constructor ...
    (comp.lang.javascript)
  • Compiler bug, surfacing in a map?
    ... I am currently writing a tool which employs a rather large map in one ... The keys are of a custom type, basically 4 wrapped ints, ... The map itself uses the Boost pool allocator, ... this has worked fine all until I introduced this copy constructor. ...
    (microsoft.public.vc.stl)