Skip to Content
Introducing C++
book

Introducing C++

by Frances Buontempo
March 2026
Beginner
348 pages
7h 27m
English
O'Reilly Media, Inc.
Content preview from Introducing C++

Chapter 11. Classes: Special Member Functions and Move Semantics

In Chapter 10, you wrote your own class and discovered that the compiler can generate special member functions for you, such as constructors and destructors. This chapter will show you the other special member functions, when they are used, and why they matter.

This chapter is a deep dive to explain some important ideas, including move semantics: a way to make your code more efficient by transferring resources. As it stands, your Stock class works well enough, but taking time to understand what is happening will give you a solid C++ foundation.

You will also learn about how a std::string works in detail, seeing what happens in each special member function. This will show you why C++ provides various special member functions for classes and what they do.

Copying Objects

In the previous chapter, in Example 10-4, you made a vector of stocks, as shown in Example 11-1.

Example 11-1. A vector of stocks from an initializer list
#include <vector>

#include "stock.h"

int main()
{
    using namespace stock_prices;
    std::vector stocks {
        Stock{"Coffee", 4.8, 0.0113},
        Stock{"Tea", 171.68, 0.023},
        Stock{"Sugar", 17.91, 0.05}
    };
}

This code calls a constructor three times but calls six destructors. The initializer list contains three objects that are copied to the std::vector. The copying happens via a special member function called a copy constructor, designed to make a copy of an existing object. Figure 11-1 shows how you end ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Effective Modern C++

Effective Modern C++

Scott Meyers
C++ Software Design

C++ Software Design

Klaus Iglberger
Learn C++ by Example

Learn C++ by Example

Frances Buontempo

Publisher Resources

ISBN: 9781098178130Errata Page