サブクラスからベースoperator=
にベース実装クラスとデリゲートを使用することができます一つの方法は、ポストコピーロジックを提供するために、派生クラスから再び導出することです。
#include <iostream>
// a wrapper class to provide custom copy actions
template<class Base>
struct CopyActions : Base
{
using base_class = Base;
using CopyActions::base_class::base_class;
// copy operator will call the base and then perform custom action
CopyActions& operator=(const CopyActions& r) {
base_class::operator=(r);
onCustomCopy(r, *this);
return *this;
}
};
// a class to notify us when a copy takes place, without having to write
// custom copy operators
struct copy_sentinel
{
copy_sentinel operator=(const copy_sentinel&) {
std::cout << "copying " << name << '\n';
return *this;
}
const char* name;
};
int test_count = 0;
// a model base class
struct MyBase
{
int base_count = test_count++;
copy_sentinel base_s { "MyBase" };
};
// a model derived class containing only logic
struct MyDerived : MyBase
{
int derived_count = test_count++;
copy_sentinel base_s { "MyDerived" };
};
// a custom copy action (free function)
void onCustomCopy(const MyDerived& from, MyDerived& to)
{
std::cout << "custom copy action\n";
}
// our derived class with custom copy actions
using SuperDerived = CopyActions<MyDerived>;
// test
int main()
{
SuperDerived a; // 0, 1
SuperDerived b; // 2, 3
// prove initial values
std::cout << a.base_count << ", " << a.derived_count << std::endl;
// perform copy and report actions
a = b;
// prove a copy occurred
std::cout << a.base_count << ", " << a.derived_count << std::endl;
}
期待される結果:
0, 1
copying MyBase
copying MyDerived
custom copy action
2, 3
は、なぜあなただけではなく、 'ClsImpl ::演算子=(その他)を介して明示的に呼び出すの基本クラスの実装を呼び出す強制的に' static_cast'を使用しています; '。 'ClsImpl'がコピーアサイン機能を委譲する単なる手段であれば、暗黙のコンバージョンを防ぎ、スライシングのリスクを導入するためにプライベート継承を使うべきでしょう。 –
@Captain彼らは同じことになりますが、キャストを省略することは利益です、私は同意します。プライベート継承は、基本クラスのメンバ関数のすべてを公開するために、大量の 'using'宣言を必要としないようであれば素晴らしいでしょう。 –
ええ、多くの 'using'ステートメントは吸うでしょう。基本クラスでコピーコンストラクタとコピー代入演算子を保護することは、とにかくスライシングを処理するでしょう。 –