C++ のコピーコンストラクターと代入演算子


 C++            
 Effective C++  Effective C++ 
(2)

 

2

  
  Foo a;
  Foo b(a);  // コピーコンスタクター
 
 使
  Foo a, c;
  c = a;     // 代入演算子
2
()
  Foo a;
  Foo d = a; // コピーコンスタクター
2


使()















memcpy 

Effective C++ (45) C++  



noncopy_sample.cpp :
class Person
{
private:
    std::string m_name;
    int m_age;
    // :
};
int 

string  string 





Effective C++ (11)  

C(const C &);
C &operator=(const C &);


copy_sample.cpp : ()
class Person
{
  char *m_name;                         ///< 名前
  unsigned int m_age;                   ///< 年齢

public:
  /// コンストラクター
  Person(const char *name = 0, unsigned int age = 0)
      :m_name(0), m_age(age)
  {
    SetName(name);
  }

  /// デストラクター.
  virtual ~Person()
  {
    SetName(0);
  }


  /// コピーコンストラクター
  Person(const Person &other)
      :m_age(other.m_age)
  {
    SetName(other.m_name);
  }

  Person &operator=(const Person &other)
  {
    // 自身の代入チェック
    if (this != &other) {      
      SetName(other.m_name);
      m_age = other.m_age;
    }
    return *this;
  }

  // :  

};

  *this  
  Person &operator=(const Person &other)
  {
      // :
    return *this;
  }
 void 
  a = b = 1; (*this)

Effective C++ (15) operator = *this  

(Method chaining)

(Method chaining) | 

 this 
  Person &operator=(const Person &other)
  {
    // 自身の代入チェック
    if (this != &other) {      
        // :
    }
    return *this;
  }



Effective C++ (17) operator =  



copy_sample.cpp : ()
class Student : public Person
{
  char *m_school;                         ///< 学校名

public:
  /// コンストラクター
  Student(const char *name = 0, unsigned int age = 0, const char *school = 0)
      :Person(name, age)
  {
    SetSchool(school);
  }

  /// デストラクター.
  virtual ~Student()
  {
    SetSchool(0);
  }

  /// コピーコンストラクター
  Student(const Student &other)
      :Person(other)
  {
    SetSchool(other.m_school);
  }

  Student &operator=(const Student &other)
  {
    // 自身の代入チェック
    if (this != &other) {
      // 基底クラスメンバーの代入
      Person::operator=(other);
      SetSchool(other.m_school);
    }
    return *this;
  }

  // :
  
};
  Student(const Student &other)
      :Person(other)  // 基底クラスのコピーコンスタクター
  {
  Student &operator=(const Student &other)
  {
      // :
      // 基底クラスメンバーの代入
      Person::operator=(other);
      SetSchool(other.m_school);


Effective C++ (16) operator =  

DRY(Don't Repeat Yourself)
使
  Person(const Person &other)
  {
    *this = other;
  }
 
使

 const 

Effective C++ (12) 使 

使 使




2

const







fstream (FILE *) 使
 fstream 使

 - 

  private 

Effective C++ (27) 使 

 private 
class Person
{  
 private:
  // コピー禁止
  Person(const Person &);
  void operator=(const Person &);
 void 

 - 

使
/// コピー禁止マクロ
/// @param C クラス名
#define NON_COPYABLE(C)       C(const C &);       \
                              void operator=(const C &)
使  pri
vate 
class Person
{
 private:
  // コピー禁止(マクロ版)
  NON_COPYABLE(Person);
C++ 


Effective C++ (27) #define const   inline 使 

 inline 

 -  (boost::noncopyable) 

private  private 
Boost 使

letsboost::noncopyable

#include <boost/noncopyable.hpp>

class Person : boost::noncopyable
{
boost::noncopyable 使

   noncopyable  boost::noncopyable 



More C++ Idioms/ (Non-copyable Mixin) - Wikibooks

 - C++11

C++11 使 delete  

C++11 - Wikipedia #  default/delete 

使
class Person
{
  public:
  // コピー禁止 (C++11)
   Person(const Person &) = delete;
   Person &operator=(const Person &) = delete;
/// コピー禁止マクロ (C++11)
/// @param C クラス名
#define NON_COPYABLE(C)         C& operator=(const C&) = delete;        \
                                C(const C&) = delete;
 private 
private 使使

 C++11 使
C++  | 



protcopy_sample.cpp





関連記事
Prev.    Category    Next 

Facebook コメント


コメント

No title

< if (this == &other) {
> if (this != &other) {
じゃない

Re: No title

> < if (this == &other) {
> > if (this != &other) {
> じゃない

指摘ありがとうございます。
確かに間違えてましたので、修正しました。

確認

こんにちは
参考になります
上のコメントで指摘されている修正内容(!=)がリンク先の.cppファイル側(copy_sample.cpp)に反映されて無い様に見えます
(html側は修正されている様です)

No title

デフォルトのコピーコンストラクタはメンバそれぞれのコピーコンストラクタを呼ぶだけでなく、配列はメモリコピーしますよね

修正事項

copy_sample.cpponline gdb



53Person
if (this != &other)
132Student



45Person
:m_name(0),m_age(other.m_age)
m_name0

108Student
:Person(name, age),m_school(0)
m_school0

124Student
:Person(other),m_school(0)
m_school0

3char
SetName()SetShool()if (m_name)if (m_school)true
newdeleteSegmentation fault.
Personm_name(0)
m_name(nullptr),m_name{nullptr}
(23,103)0nullptr



71150Setterdelete
delete[] m_name
delete[] m_school

m_name,m_schoolSetternewleng+1
delete



183
cout << michael << endl; // {"Michael"(21)}-"Foo University"

cout << tom << endl; // {"Tom"(21)}-"Foo University"

コメントの投稿

Font & Icon
非公開コメント

このページをシェア
アクセスカウンター
アクセスランキング
[ジャンルランキング]
コンピュータ
38位
アクセスランキングを見る>>

[サブジャンルランキング]
プログラミング
7位
アクセスランキングを見る>>
カレンダー(アーカイブ)
プルダウン 降順 昇順 年別

06月 | 2024年07月 | 08月
- 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 - - -


はてな新着記事
はてな人気記事
ブロとも申請フォーム
プロフィール

yohshiy

Author:yohshiy
職業プログラマー。
仕事は主に C++ ですが、軽い言語マニアなので、色々使っています。

はてブ:yohshiy のブックマーク
Twitter:@yohshiy

サイト紹介