Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's exactly as you said.</p> <p>When you pass an object by value, its <em>copy constructor</em> is invoked to produce a new instance of such object that will be used inside the function. The changes done to such new object won't be reflected to the original one<sup>1</sup>.</p> <p>As with structures, the default copy constructor just does a shallow copy of the original object - i.e., its fields are copied<sup>2</sup> to the new instance; in many cases this is not desirable (e.g. if the object wraps a pointer/another resource), so there are classes which redefine the copy constructor or disable it completely. Objects of these last classes can only be passed by pointer or reference.</p> <p>Passing objects by value can be costly if they are bigger than a pointer (in size) or in general if their copy constructor isn't "cheap". On the other hand, in comparison to pointers, the pass-by-value yields the usual advantages of not having to specify the pointer ownership, letting the callee do whatever it wants with the object, etc.</p> <p>Notice that passing an object by value kills the polymorphism. This because a function receiving an object by value receives a statically typed object, with a precise size and type, so any attempt to pass an object of a derived class will result in object slicing (the copy constructor for the base class is called, that by default just copies the fields that are available in the base class).</p> <p>This is the reason why often the preferred method of passing objects is by <code>const</code> reference. This yields several advantages:</p> <ul> <li>no copies involved; the object that the callee will see will be exactly the one specified at the moment of the call;</li> <li>no changes to the original object can be made, thanks to the <code>const</code> qualifier;</li> <li>if however the callee needs to change a copy of the object, it can still construct a copy by itself from the reference;</li> <li>no awkward pointer syntax;</li> <li>polymorphism preserved, since behind the scenes we're actually passing a pointer;</li> <li>no big doubts about object ownership: the general rule about references is that they are owned by the caller.</li> </ul> <hr /> <ol> <li><p>As far as the "raw fields" of the object are concerned; naturally if the original object and the copy continue to share a pointer/handle to the same resource some modifications to one may affect the other.</p></li> <li><p>Primitive types (and in general PODs) are copied bitwise, while the copy constructor is called for non-POD types.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload