set std::map value from within function without deleting on return

So let's say we have this code:

1
2
3
4
5
6
7
8
9
10
11
std::map<std::string, SomeClass> exampleMap;

void addToExampleMap() {
  exampleMap["foo"] = SomeClass();
}

int main() {
  addToExampleMap();
  exampleMap["foo"].doSomething(); //error because the object does not exist
  return 0;
}


Obviously this is very oversimplified, but when I run something like this, the new SomeClass object that I set "foo" to is being deleted at the end of the function since it is a local variable (at least that's what I think is happening). How would I add that empty SomeClass to exampleMap without letting it get deleted after the function exits?
Last edited on
The new SomeClass object that I set "foo" to is being deleted at the end of the function

No, it's not. There's no error here, unless SomeClass is wrong.

Edit: assignment makes a copy of the temporary SomeClass (or moves it in this case, if available). There are no local variables in addToExampleMap().
Last edited on
You're right I'm sped. I was doing something really weird. Thanks for confirming though.
Topic archived. No new replies allowed.