如何在纯抽象类中实现克隆? [英] How to implement clone in a pure abstract class?

查看:24
本文介绍了如何在纯抽象类中实现克隆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想在我的派生类中覆盖纯抽象方法,但我收到了这个错误.谁能帮我看看发生了什么,我该如何完成.

So I want to override the pure abstract method in my derived classes but I got this error. Can someone help me see what happened and how can I complete it.

我的设备类;

class Device  {
public:
    Device();
    Device(const Device& orig);
    virtual ~Device();

    virtual Device Clone() = 0;
}

还有我的派生类;

class Radar : public Device {
public:
    Radar();
//    Radar(const Radar& orig); // commenting so the compiler using its default copy constructor
    virtual ~Radar();

    Radar Clone();    
};

我的Radar 类的源文件;

Radar Radar::Clone() {
    return *(new Radar(*this));
}

如果我在 Device 类的 Clone 方法中使用 Device 类型,它会弹出那个 Device 是一个抽象类.

If I use Device type in my Clone method in Device class, it will pop-up that Device is an abstract class.

如果我使用 void 类型(我假设它不是我想要的),它会表明我没有实现这个方法.

If I use void type (which I'm assuming it's not what I want to have), it will show that I haven't implement this method.

我该怎么办?

推荐答案

您的 Clone 方法将需要返回指向克隆对象的指针...协变返回类型只能以这种方式工作(如返回value 要求调用者将返回的值复制到堆栈 - 当您使用 new 分配它时,这将是内存泄漏).

Your Clone method will need to return pointers to the cloned objects... covariant return types only work that way (as returning by value is asking the caller to copy the returned value to the stack - that would be a memory leak when you've allocated it with new).

所以,应该是:

virtual Device* Clone() = 0;

……然后……

Radar* Clone();    // YES, it should be Radar* here - that uses C++'s support for
                   // "covariant return types", see also "UPDATE" discussion

Radar* Radar::Clone()
{
    return new Radar(*this);
}

更新 - 根据要求进一步解释

UPDATE - further explanation as requested

因此,克隆函数的想法是它可以返回您的 Device* 当前正在寻址的任何实际派生类型的深层副本.鉴于派生类型可能会添加 Device 缺少的数据成员,它可能是一个更大的对象,并且调用者无法保留适量的堆栈空间来存储它.出于这个原因,对象需要使用 new 动态分配,并且可预测大小的 Device* 是调用者访问新对象的方式.不过,克隆函数返回一个 Radar* 是合法的——这意味着客户端代码在编译时知道它正在处理一个 Radar 并且可以克隆它继续将其用作 Radar - 访问 Radar 提供的任何额外成员.

So, the idea with a clone function is that it can return a deep copy of whatever actual derived type your Device* is currently addressing. Given that derived type might add data members that Device lacked, it could be a larger object, and the caller has no ability to reserve the right amount of stack space in which to store it. For that reason, the object needs to be allocated dynamically with new, and the predictably-sized Device* is the caller's way to access the new object. It's legal for the clone function to return a Radar* though - all that means is that client code that knows at compile time that it is dealing with a Radar and clones it can continue to use it as a Radar - accessing any extra members that Radar provides.

希望有助于澄清事情.您可能还想阅读一些有关面向对象编程的背景资料.

Hope that helps clarify things. You might also want to do some background reading on Object Oriented programming.

这篇关于如何在纯抽象类中实现克隆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆