c++ - STL Scope of containers -
i wanted know of scope of stl containers.
for eg. //have function creates unordered_map , passes set_map fill values in it.
int foo() { unorderd_map<char,int>mymap; set_map(mymap); } set_map (unorderd_map<char,int> mmap){ //...setting values of map }
in case scope of mymap in foo limited function foo() or mymap passed reference set_map() , whatever changes done in set_map reflected mymap in foo() ?
i wanted know how still containers passed function parameters, i.e. passed value or passed reference.
thank you
mymap
passed as copy set_map
, set_map
sees own copy of map, not original mymap
. changes made in set_map
applied copy, , not affect original mymap
.
to pass map reference need declare explicitly:
set_map (unordered_map<char,int>& mmap) // ^ passed reference now.
now changes in set_map
alter original object passed parameter.
Comments
Post a Comment