Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
–
–
–
No.
myMap.clear()
will destroy its own
copy
of
myInsideMap
.
myMap
will cease to exist when the
constructor()
scope is exited.
Standard library containers own their elements. When they are cleared or cease to exist, their elements are destroyed too.
Quoting a note from the standard section 23.2.1 that talks about General container requirements:
the destructor is applied to every element of a; all the memory is deallocated.
I expect myMap.clear() will clear myInsideMap too
Nope. myMap clears the contents of mapMap only which includes a std::pair object internally which in turn has a copy of myInsideMap.
myInsideMap was allocated on the stack and hence it is deallocated when its local scope is over when constructor() exits. Again, this is not where the myInsideMap (copy) inside the myMap is destructed.
is this cleared correctly?? recursively?
Yes, myMap is cleared here but the recursive destruction happens as follows - the std::pair destructor is called internally for which the key and values are destructed. Since, the copy of myInsideMap was the value within this pair structure, that content is also destroyed. So the destructive "recursion" doesn't touch the local or the actual myInsideMap in constructor().
Thanks for contributing an answer to Stack Overflow!
-
Please be sure to
answer the question
. Provide details and share your research!
But
avoid
…
-
Asking for help, clarification, or responding to other answers.
-
Making statements based on opinion; back them up with references or personal experience.
To learn more, see our
tips on writing great answers
.