Re: std::map question
From: David Hilsee (davidhilseenews_at_yahoo.com)
Date: 08/14/04
- Next message: David Hilsee: "Re: std::map question"
- Previous message: Peter Olcott: "Re: Yet another Attempt at Disproving the Halting Problem"
- In reply to: Flzw: "std::map question"
- Next in thread: David Hilsee: "Re: std::map question"
- Reply: David Hilsee: "Re: std::map question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: David Hilsee: "Re: std::map question"
- Previous message: Peter Olcott: "Re: Yet another Attempt at Disproving the Halting Problem"
- In reply to: Flzw: "std::map question"
- Next in thread: David Hilsee: "Re: std::map question"
- Reply: David Hilsee: "Re: std::map question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|