It's not just that it's being "copied", it's just that it's a completely independent object.
models::dropLocation_t drop_location;
This creates a completely independent, separate object, called drop_location
.
Anything that's changed in this object, any member of this class instance that get modified, affects this object only. It will never have any effect on any other object that might exist somewhere.
getDropLocationByName(prod_config, query_drop_location_name, drop_location);
drop_location.materials = materials;
The effect of the function call appears to be to find some particular models::dropLocation_t
instance that exists in prod_config
and then copy it into drop_location
. Ok, so something from prod_config
was copied into drop_location
. Great. Then drop_location.materials
get modified. So far so good. The end result: some object from prod_config
gets copied into drop_location
, and then something else in drop_location
called "material` gets modified.
And that's the end of the story. You seem to expect that just because that something was originally copied from prod_config
into drop_location
then this must mean that this modification affects the original object. That's approximately how objects might behave in Java or C#, but C++ is neither one of those two languages. Reduced into the most simple form, this is nothing more than:
int c=0;
int b=c;
b=3;
c
is still 0, only b
gets changed to 3. Just because b
's value originally came from c
doesn't mean that this also changes c
to 3. The same thing applies to the shown code, as well. drop_location.materials=materials;
, where drop_location
is an object, only affects an object called drop_location
, and no other object. The End. It does not affect anything else, and has no effect on whichever object the rest of drop_location
may or may not have been copied from. drop_location
is an independent object. It's not that its copied (and it was), it's just all objects in C++ are, well, discrete objects. In other languages, like Java, all objects are really pointers, so copying a so-called "object" from one place to another, and then modifying the "copied" object also ends up modifying the original "object", but that's not how objects work in C++.