我在典型的菱形问题中使用虚拟继承:
A
(virtual) / \ (virtual)
B C
\ /
D
我在每个类中都实现了一个名为“deep_copy_from”的方法(但它也可以是赋值运算符=())。该方法应该复制类自己的属性,并将副本传播到上面的类。
问题是 A::deep_copy_from 方法在我深度复制 D 实例时被调用了两次(它应该只被调用一次,因为 A 只有一个“版本”)。确保它只被调用一次的最佳方法是什么?
(B::deep_copy_from 和 C::deep_copy_from 应该继续以相同的方式工作)。
这是一个示例代码:
class A
{
public:
A(string const& p_a_name) : a_name(p_a_name) {
cout << "A(a_name=\"" << p_a_name << "\")" << endl;
}
virtual void deep_copy_from(A const& a)
{
cout << "A::deep_copy_from(A(a_name=\"" << a.a_name << "\"))" << endl;
this->a_name = a.a_name;
}
protected:
string a_name;
};
class B : public virtual A
{
public:
B(string const &p_a_name, string const& p_b_name) : A(p_a_name), b_name(p_b_name) {
cout << "B(a_name=\"" << p_a_name << "\", b_name=\"" << p_b_name << "\")" << endl;
}
virtual void deep_copy_from(B const& b)
{
cout << "B::deep_copy_from(B(a_name=\"" << b.a_name << "\", b_name=\"" << b.b_name << "\"))" << endl;
this->A::deep_copy_from(static_cast<A const&>(b));
this->b_name = b.b_name;
}
protected:
string b_name;
};
class C : public virtual A
{
public:
C(string const &p_a_name, string const& p_c_name) : A(p_a_name), c_name(p_c_name) {
cout << "C(a_name=\"" << p_a_name << "\", c_name=\"" << p_c_name << "\")" << endl;
}
virtual void deep_copy_from(C const& c)
{
cout << "C::deep_copy_from(C(a_name=\"" << c.a_name << "\", c_name=\"" << c.c_name << "\"))" << endl;
this->A::deep_copy_from(static_cast<A const&>(c));
this->c_name = c.c_name;
}
protected:
string c_name;
};
class D : public B, public C
{
public:
D(string const &p_a_name, string const& p_b_name, string const& p_c_name, string const& p_d_name)
: A(p_a_name), B(p_a_name, p_b_name), C(p_a_name, p_c_name), d_name(p_d_name)
{
cout << "D(a_name=\"" << p_a_name << "\", b_name=\"" << p_b_name
<< "\", c_name=\"" << p_c_name << "\", d_name=\"" << p_d_name << "\")" << endl;
}
virtual void deep_copy_from(D const& d)
{
cout << "D::deep_copy_from(D(a_name=\"" << d.a_name << "\", b_name=\"" << d.b_name
<< "\", c_name=\"" << d.c_name << "\", d_name=\"" << d.d_name << "\"))" << endl;
this->B::deep_copy_from(static_cast<B const&>(d));
this->C::deep_copy_from(static_cast<C const&>(d));
this->d_name = d.d_name;
}
protected:
string d_name;
};
这是当前的输出:
A(a_name="A")
B(a_name="A", b_name="B")
C(a_name="A", c_name="C")
D(a_name="A", b_name="B", c_name="C", d_name="D")
D::deep_copy_from(D(a_name="A", b_name="B", c_name="C", d_name="D"))
B::deep_copy_from(B(a_name="A", b_name="B"))
A::deep_copy_from(A(a_name="A"))
C::deep_copy_from(C(a_name="A", c_name="C"))
A::deep_copy_from(A(a_name="A"))
更新:
现在的版本是:
class A
{
public:
A(string const& p_a_name) : a_name(p_a_name) {
cout << "A(a_name=\"" << p_a_name << "\")" << endl;
}
virtual void deep_copy_from(A const& a)
{
cout << "A::deep_copy_from(A(a_name=\"" << a.a_name << "\"))" << endl;
this->a_name = a.a_name;
}
protected:
string a_name;
};
class B : public virtual A
{
public:
B(string const &p_a_name, string const& p_b_name) : A(p_a_name), b_name(p_b_name) {
cout << "B(a_name=\"" << p_a_name << "\", b_name=\"" << p_b_name << "\")" << endl;
}
virtual void deep_copy_from(B const& b)
{
cout << "B::deep_copy_from(B(a_name=\"" << b.a_name << "\", b_name=\"" << b.b_name << "\"))" << endl;
this->A::deep_copy_from(static_cast<A const&>(b));
this->deep_copy_my_bits_from(b);
}
protected:
void deep_copy_my_bits_from(B const& b) {
cout << "B::deep_copy_my_bits_from(B(a_name=\"" << b.a_name << "\", b_name=\"" << b.b_name << "\"))" << endl;
this->b_name = b.b_name;
}
protected:
string b_name;
};
class C : public virtual A
{
public:
C(string const &p_a_name, string const& p_c_name) : A(p_a_name), c_name(p_c_name) {
cout << "C(a_name=\"" << p_a_name << "\", c_name=\"" << p_c_name << "\")" << endl;
}
virtual void deep_copy_from(C const& c)
{
cout << "C::deep_copy_from(C(a_name=\"" << c.a_name << "\", c_name=\"" << c.c_name << "\"))" << endl;
this->A::deep_copy_from(static_cast<A const&>(c));
this->deep_copy_my_bits_from(c);
}
protected:
void deep_copy_my_bits_from(C const& c) {
cout << "C::deep_copy_my_bits_from(C(a_name=\"" << c.a_name << "\", c_name=\"" << c.c_name << "\"))" << endl;
this->c_name = c.c_name;
}
protected:
string c_name;
};
class D : public B, public C
{
public:
D(string const &p_a_name, string const& p_b_name, string const& p_c_name, string const& p_d_name)
: A(p_a_name), B(p_a_name, p_b_name), C(p_a_name, p_c_name), d_name(p_d_name)
{
cout << "D(a_name=\"" << p_a_name << "\", b_name=\"" << p_b_name
<< "\", c_name=\"" << p_c_name << "\", d_name=\"" << p_d_name << "\")" << endl;
}
virtual void deep_copy_from(D const& d)
{
cout << "D::deep_copy_from(D(a_name=\"" << d.a_name << "\", b_name=\"" << d.b_name
<< "\", c_name=\"" << d.c_name << "\", d_name=\"" << d.d_name << "\"))" << endl;
this->A::deep_copy_from(static_cast<A const&>(d));
this->B::deep_copy_my_bits_from(static_cast<B const&>(d));
this->C::deep_copy_my_bits_from(static_cast<C const&>(d));
this->d_name = d.d_name;
}
protected:
string d_name;
};
输出是:
A(a_name="A")
B(a_name="A", b_name="B")
C(a_name="A", c_name="C")
D(a_name="A", b_name="B", c_name="C", d_name="D")
D::deep_copy_from(D(a_name="A", b_name="B", c_name="C", d_name="D"))
A::deep_copy_from(A(a_name="A"))
B::deep_copy_my_bits_from(B(a_name="A", b_name="B"))
C::deep_copy_my_bits_from(C(a_name="A", c_name="C"))
我能得到比这更好的吗?(即,更自动)