トップページ – Modern C++編 C++編](../../index.html) – 第14章
問題① この章で使った Nameクラスを、コピーとムーブに対応した形で完成させてください。
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <utility>
class Name {
public:
(const char* name)
Name{
= static_cast<char*>(std::malloc(std::strlen(name) + 1));
mName std::strcpy(mName, name);
}
(const Name& rhs) : mName(nullptr)
Name{
(rhs);
Copy}
(Name&& rhs) : mName(nullptr)
Name{
*this = std::move(rhs);
}
~Name()
{
std::free(mName);
}
& operator=(const Name& rhs)
Name{
(rhs);
Copyreturn *this;
}
& operator=(Name&& rhs)
Name{
if (this != &rhs) {
= rhs.mName;
mName .mName = nullptr;
rhs}
return *this;
}
const char* GetName() const
{
return mName;
}
private:
void Copy(const Name& rhs)
{
// 新しい領域を作り、内容をコピー
char* const n = static_cast<char*>(std::malloc(std::strlen(rhs.mName) + 1));
std::strcpy(n, rhs.mName);
// 以前の領域を解放
std::free(mName);
// 新しい領域を指すようにポインタを付け替える
= n;
mName }
private:
char* mName;
};
int main()
{
= "Ken"; // コンストラクタ
Name name1 = name1; // コピーコンストラクタ
Name name2 = std::move(name1); // ムーブコンストラクタ
Name name3
std::cout << name2.GetName() << std::endl;
std::cout << name3.GetName() << std::endl;
= name3; // コピー代入
name1 = std::move(name3); // ムーブ代入
name2 std::cout << name1.GetName() << std::endl;
std::cout << name2.GetName() << std::endl;
}
実行結果:
Ken
Ken
Ken
Ken
コピーとムーブに関するメンバ関数を明示的に実装するのなら、デストラクタも実装するべきですし、大抵は必要性があります。
なお、ムーブを行った後、ムーブ元になったオブジェクトは使ってはいけません。
’2017/8/27 新規作成。
Programming Place Plus のトップページへ
はてなブックマーク に保存 | Pocket に保存 | Facebook でシェア |
X で ポスト/フォロー | LINE で送る | noteで書く |
RSS | 管理者情報 | プライバシーポリシー |