我有一个基类与虚拟克隆新方法如何轻松编写克隆方法?
class A
{
virtual A* cloneNew() const { return new A; }
};
及其衍生物
class A1 : public A
{
virtual A1* cloneNew() const { return new A1; }
};
class A2 : public A
{
virtual A2* cloneNew() const { return new A2; }
};
现在我想用宏或其他方式使其重新实现更容易喜欢
class A1: public A
{
CLONE_NEW; // no type A1 here
};
可以做到这一点吗? decltype(这个)有帮助吗?
拥有克隆方法通常表示设计中存在错误(代码异味)。你真的想做什么? –
@Loki Astari你能否详细说明为什么克隆方法是代码味道? – Max
@LokiAstari我在GUI的树中有动态类型的项目。我想在单击某个项目时创建一个新的类似项目。 – user1899020