liveBook · Manning

Call by value

As explained in Object-Oriented Software Design in C++

A parameter-passing mechanism that initializes a function parameter with a copy of the argument value.

Pass by value initializes a function parameter with a copy of the argument. In C++, that copy can invoke a copy constructor and later a destructor. section 6.2.2

Passing objects by value can create hidden runtime work:

  • Calling append with a Date by value copies the argument before vector insertion. section 6.2.2
  • Passing Appointments by value copies the container and its Date elements before print executes. section 6.2.2
  • Vector growth can copy existing elements, then deallocate the old array and temporary objects. section 6.2.2

Instrumented Date and Appointments

An instrumented Date class prints constructor, copy-constructor, assignment, and destructor calls. Running append and print exposes repeated copies, allocations, and deallocations that ordinary output hides.

How Object-Oriented Software Design in C++ covers it

How deep it goes
Traces copy construction, vector growth, allocation, and destruction, then stops short of a complete alternative design.
Examples
Demonstrates Date arguments passed to append and Appointments passed to print, using instrumented lifecycle output.
What you can do after
Diagnose performance problems by identifying unexpected copies, allocations, and destructions in instrumented C++ programs.
How it connects
Connects pass by value with copy constructors, destructors, vector growth, dynamic allocation, and pointers.

Also explained in

Compare how 3 books explain call by value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.