Skip to Content
C++の紹介 (Japanese Edition)
book

C++の紹介 (Japanese Edition)

by Frances Buontempo
March 2026
Intermediate
348 pages
4h 30m
Japanese
O'Reilly Media, Inc.
Content preview from C++の紹介 (Japanese Edition)

第11章 クラス :特殊メンバ関数移動セマンティクス

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

第10章では、独自のクラスを作成し、コンパイラがコンストラクタやデストラクタといった特別なメンバ関数を自動生成できることを学んだ。 本章では、のその他の特別なメンバ関数、それらの使用場面、そして重要性を説明する。

本章ではを深く掘り下げ、リソースの転送によってコードの効率性を高める方法である移動セマンティクスを含む重要な概念を説明する。 現状のStockクラスは十分に機能しているが、その動作を理解する時間を割くことで、C++の確固たる基礎が築ける。

また、std::stringがどのように動作するかを詳細に学び、各特殊メンバ関数で何が起きているのかを確認する。 これにより、C++ がクラスに様々な特殊メンバ関数を提供する理由とその役割が明らかになる。

オブジェクトのコピー

前の章の例 10-4 では、例 11-1 に示すように、のベクトルに株式を格納した。

例 11-1. 初期化リストからの株式ベクトル
#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}
    };
}

このコードはコンストラクタを3回呼び出すが、デストラクタは6回呼び出される。 初期化リストには3つのオブジェクトが含まれており、これらがstd::vectorにコピーされる。 コピーはコピーコンストラクタと呼ばれるの特殊メンバー関数によって行われる。これは既存のオブジェクトのコピーを作成するために設計されている。 図11-1はコピー時に2つのオブジェクトが存在する状態を示している。したがって、元の3つのオブジェクトと3つのコピーをすべて破棄する必要がある。

Diagram illustrating the copy process, showing an original object and a copy constructed object created from data members.
図 11-1. オブジェクトのコピー

コピーの詳細を見てみよう。 コンパイラは次のようなシグネチャでコピーコンストラクタを提供できる:

Stock(const Stock & other);

これまで使ってきた他のコンストラクタと同様、コピーコンストラクタもクラス名を持つ。 単一のパラメータ(コピー元となる他のオブジェクト)を受け取り、新しいインスタンスを構築する。 パラメータotherは既存のオブジェクトであるため、この関数内で一部または全てのメンバのコピーを取ることができる。 独自のコピーコンストラクタを書くことも可能だが、ここでは必要ない。 デフォルトのものが目的を果たす。

デフォルトのコピーコンストラクタは特定の条件下でのみ生成されるため、明示的にコピーコンストラクタをリクエストできると便利だ。デフォルトを使用してコピーコンストラクタをリクエストできる:

Stock(const Stock & other) = default;

また、`削除` を使ってコピーを無効化することもできる:

Stock
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

エージェントメッシュ (Japanese Edition)

エージェントメッシュ (Japanese Edition)

Eric Broda, Davis Broda

Publisher Resources

ISBN: 0642572352653