アプリ版:「スタンプのみでお礼する」機能のリリースについて

<仮想基底クラスをもつクラスの代入演算>の続きですが、結局、assign のようなものを作って、以下のように書くのが一番いいのでしょうか。。。
====(一応、自己代入も考慮して書くと^^)
#include <algorithm>

struct X {
int x;
X &assign(const X &z, bool top) {
if (this != &z) x = z.x;
return *this;
}
X &operator=(const X &z) { return assign(z, true); }
};
struct A : virtual X
{
int a;
A &assign(const A &z, bool top) {
if (this != &z) {
if (top) X::assign(z, false);
a = z.a;
}
return *this;
}
A &operator=(const A &z) { return assign(z, true); }
};
struct B : virtual X
{
int b;
B &assign(const B &z, bool top) {
if (this != &z) {
if (top) X::assign(z, false);
b = z.b;
}
return *this;
}
B &operator=(const B &z) { return assign(z, true); }
};

struct C : A, B
{
int c;
C &assign(const C &z, bool top) {
if (this != &z) {
if (top) X::assign(z, false);
A::assign(z, false);
B::assign(z, false);
c = z.c;
}
return *this;
}
C &operator=(const C &z) { return assign(z, true); }
};
====
ただ、<仮想基底クラスをもつクラスの代入演算>で、jacta さんが提示してくださった方法も、処理量は増えますが、アロケートする必要があるポインタなどのデータメンバのアロケートやデリートする処理コードをコンストラクタに集中できるので、「コーディング量の減少・改変の際の修正し忘れの可能性を低くできる」というメリットがあると思います。ハンドルクラスやコンテナクラスを実装する際は、swap() 機能が必要となると考えられるので、jacta さんのコピーコンストラクタでコピー代入を実装する方法の方がいいのかとも思います。

みなさん、どう思われますか?
また、そのほかに、効果的なテクニックがあれば、ぜひ、ご教示ください。

A 回答 (1件)

これが一番いいということで^^

    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!