What part of C ++ creates many "copies" of objects?

Although I develop C ++ primarily, I don't see the C ++ joke here. Is it a joke that passing by value is the default behavior and therefore creates many copies? Or is there some other part of C ++ that has built this reputation?

enter image description here

+3


source to share


1 answer


Compared to other OO languages, C ++ makes heavy use of value semantics. Type code Object B = A;

in many languages โ€‹โ€‹creates a new reference, but not a new object. We call this reference semantics. In these languages, you need to write something like Object B = new Object(A)

to make a copy. But with semantics, the meaning Object B = A;

is actually a copy.



The second reason was that C ++ doesn't use garbage collection. This means that you often need a copy of an object to guarantee its lifespan. (Today we have std::shared_ptr

)

+3


source







All Articles