C++ Foundations·Modern C++ without the folklore

Value semantics and copy

In C++ an object is a value, and copying makes a fully independent duplicate.

Intermediate 13 min read Lesson 1 of 300 Challenge included
What you will be able to do
  • Copying a C++ object makes a fully independent duplicate with its own storage.
  • Pass by const reference to read without copying; pass by value only for cheap types.
  • A stack object is destroyed automatically when its scope ends.
  • Copy construction and copy assignment are two different operations.

In plain English

In C++ an object is a value, which means a variable owns its own storage and copying it makes a full, independent duplicate. A stack object lives inside a function and is destroyed automatically when the function returns, which is the simplest kind of ownership. When you copy an object, the copy constructor runs, and when you assign to an existing object, the copy assignment operator runs instead. Passing by value makes a copy, while passing by reference lets the function use the original object without copying it.

Step by step

  1. 1Declare a class that owns a std::string member.
  2. 2Add a copy constructor that copies every member from another instance.
  3. 3Add a copy assignment operator that copies members into an existing object.
  4. 4Write one function that takes the object by const reference and another that takes it by value.
  5. 5Observe that by-value makes a copy while by-reference does not.
course.cppC++
#include <iostream>
#include <string>

class Course {
public:
    Course(std::string title) : title_(title) {}

    Course(const Course& other) : title_(other.title_) {}

    Course& operator=(const Course& other) {
        title_ = other.title_;
        return *this;
    }

    const std::string& title() const { return title_; }

private:
    std::string title_;
};

void read(const Course& course) {
    std::cout << course.title() << "\n";
}

void keep(Course course) {
    std::cout << course.title() << "\n";
}

int main() {
    Course a{"HTML"};
    Course b = a;
    Course c{"CSS"};
    c = a;
    read(a);
    keep(a);
}

Common mistakes

  • Passing a large object by value copies it needlessly, which slows the program down.
  • Confusing copy construction with copy assignment is common; construction builds a brand new object while assignment overwrites one that already exists.
  • Forgetting to copy a member in your copy constructor leaves that field with the wrong value in the copy.
  • Assuming a copy shares storage with the original is wrong; a correct copy is fully independent.
Your turn

Copy a lesson safely

Easy

Write a class Lesson with a std::string title member, a copy constructor that copies the title, and a copy assignment operator that copies the title into an existing object. Return *this from the assignment.

CPP · starter
class Lesson {
public:
    Lesson(std::string title) : title_(title) {}
private:
    std::string title_;
};
Check your understanding before moving on.
Knowledge checkQuestion 1

What is the key difference between pass-by-value and pass-by-const-reference?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Move semantics
Lesson 1 of 300 · 0% of course