多态对象复制
Polymorphic object copying
以下模式是古老而众所周知的。
class A
{
public:
virtual A* clone() const
{
return new A(*this);
}
private:
int x = 666;
};
class B : public A
{
public:
virtual B* clone() const override
{
return new B(*this);
}
private:
int y = 777;
};
现在我想以多态方式从 other 对象复制对象。我希望能够:
- 通过从类型 B 的对象复制来创建类型 A 的对象
- 通过从类型 A 的对象复制来创建类型 A 的对象
- 通过从类型 A 的对象复制来创建类型 B 的对象
- 通过从类型 B 的对象复制来创建类型 B 的对象
我知道案例 1 包括切片,但这是需要的。
我也知道情况 3 不会复制 y
,这也是需要的(我想使用默认的初始值)。
所有 4 种情况都应该完成多态...知道目标类型但不知道实际源对象类型。
如何在不使用 RTTI 的情况下扩展上述模式(或者替代模式看起来如何)?
据我了解,你可以
class B; // Forward declaration needed for A::CreateB
class A
{
public:
A() = default;
A(const A&) = default; // A(b) will do slicing as expected.
virtual ~A() = default;
virtual A* clone() const { return new A(*this); }
virtual B CreateB() const; // Implementation should go after B definition.
private:
int x = 666;
};
class B : public A
{
public:
B() = default; // previous constructor
B(const A&a) : A(a) {} // Create B from A (non polymorphic)
B(const B&) = default; // Copy constructor (non polymorphic)
virtual B* clone() const override { return new B(*this); }
virtual B CreateB() const { return B(*this); }
private:
int y = 777;
};
B A::CreateB() const { return B(*this); }
- Create objects of type A by copying from objects of type B
- Create objects of type A by copying from objects of type A
这两个不需要多态性,A(constA&)
就可以了
- Create objects of type B by copying from objects of type A
- Create objects of type B by copying from objects of type B
这两个由 virtual CreateB
处理,转发给 B
的构造函数(类似于访问者模式)。
以下模式是古老而众所周知的。
class A
{
public:
virtual A* clone() const
{
return new A(*this);
}
private:
int x = 666;
};
class B : public A
{
public:
virtual B* clone() const override
{
return new B(*this);
}
private:
int y = 777;
};
现在我想以多态方式从 other 对象复制对象。我希望能够:
- 通过从类型 B 的对象复制来创建类型 A 的对象
- 通过从类型 A 的对象复制来创建类型 A 的对象
- 通过从类型 A 的对象复制来创建类型 B 的对象
- 通过从类型 B 的对象复制来创建类型 B 的对象
我知道案例 1 包括切片,但这是需要的。
我也知道情况 3 不会复制 y
,这也是需要的(我想使用默认的初始值)。
所有 4 种情况都应该完成多态...知道目标类型但不知道实际源对象类型。
如何在不使用 RTTI 的情况下扩展上述模式(或者替代模式看起来如何)?
据我了解,你可以
class B; // Forward declaration needed for A::CreateB
class A
{
public:
A() = default;
A(const A&) = default; // A(b) will do slicing as expected.
virtual ~A() = default;
virtual A* clone() const { return new A(*this); }
virtual B CreateB() const; // Implementation should go after B definition.
private:
int x = 666;
};
class B : public A
{
public:
B() = default; // previous constructor
B(const A&a) : A(a) {} // Create B from A (non polymorphic)
B(const B&) = default; // Copy constructor (non polymorphic)
virtual B* clone() const override { return new B(*this); }
virtual B CreateB() const { return B(*this); }
private:
int y = 777;
};
B A::CreateB() const { return B(*this); }
- Create objects of type A by copying from objects of type B
- Create objects of type A by copying from objects of type A
这两个不需要多态性,A(constA&)
就可以了
- Create objects of type B by copying from objects of type A
- Create objects of type B by copying from objects of type B
这两个由 virtual CreateB
处理,转发给 B
的构造函数(类似于访问者模式)。