简体   繁体   English

如何为抽象类正确制作深层副本?

[英]How to correctly make deep copy for abstract classes?

I'm working on a collision system for which I need to copy the colliders of entities. 我正在研究一个碰撞系统,我需要为其复制实体的对撞机。

I make the system such as I don't have to set in stone how I want to handle collision, (and I will start using AABB likely, but might change to SAT) but I will need to make deep copy of colliders whichever algo I will use. 我制作了这样的系统,例如我不必一成不变地想如何处理碰撞(而且我可能会开始使用AABB,但可能会更改为SAT),但是无论如何我都需要制作对撞机的深层副本将使用。

on one hand, deep copy is a requirement, and the copy and swap idiom seemed to be what I should go to. 一方面,必须进行深度复制,而复制和交换的习惯用法似乎是我应该去的地方。

On the other hand, my collidable doesn't need to be anything other than an interface, so there should be no reason not to do it pure virtual. 另一方面,我的可collidable不需要界面以外的任何东西,因此应该没有理由不进行纯虚拟操作。

therefore, I began by writing this : 因此,我首先写成这样:

class collidable
{
  public:
    virtual collidable& operator= (collidable other)=0;
};

But I can't copy construct collidable because it is pure virtual. 但我不能复制构造collidable ,因为它是纯虚。

Note than within a same program, I will never use more than one algorithm of collision at the same time, so there wont be conflict of methods. 请注意,在同一程序中,我绝不会同时使用多个碰撞算法,因此不会出现方法冲突。

I don't really know what I'm doing wrong, if its the design side or the technical side, so I'm completely open to suggestions. 我真的不知道我在做什么错,无论是设计方面还是技术方面,所以我完全愿意提出建议。

How can I force class derived of collidable to implement operator= ? 如何强制将可碰撞类派生到实现operator =中?

You're probably confusing the use of an interface with a class implementation. 您可能会使接口与类实现的使用混淆。 This is likely because pure virtual classes are how C++ defines an interface. 这可能是因为纯虚类是C ++定义接口的方式。

With your implementation you're not going to have much luck because you'll get these types of scenarios: 在您的实现中,您将不会遇到太多的运气,因为您将获得以下类型的场景:

class A : public collidable
{
  ...
}

class B : public collidable
{
  ...
}

int main (int argc, char** argv)
{
    collidable *A = new A ();
    collidable *B = new B ();

    *A = *B; 
    ...
}

This would obviously be a problem. 这显然是一个问题。 What you want to do is define generic operations that would apply to each different algorithm implementation in your interface. 您要做的是定义将应用于界面中每个不同算法实现的通用操作。 Assignment would likely not be one of those generic operations because assignment needs to be done with two of the same types. 赋值可能不会是这些通用操作之一,因为赋值需要使用两种相同的类型来完成。

If you are only ever using one algorithm type as you stated, make the consumer classes of each algorithm class templated classes. 如果您仅使用了所声明的一种算法类型,则将每个算法类的使用者类作为模板类。 Then when you're operating on your algorithm implementations and call operator= on each algorithm class, the compiler should force operator= for each algorithm implementation during linking. 然后,当您在算法实现上进行操作并在每个算法类上调用operator =时,编译器应在链接期间对每个算法实现强制使用operator =。 An algorithm implementation that doesn't have operator= defined simply won't be able to link. 没有定义operator =的算法实现将无法链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM