C ++依赖于派生类的纯虚函数

詹斯·莫德维格(Jens Modvig)

我正在开发一个边界框/碰撞检测系统,并且正在使用不同类型的边界卷,像所有边界卷一样使用id来派生相同的基类,然后使用纯虚函数强制所有派生类实现诸如

  • isCollidingWith(BoudingBox)

但这给我带来了麻烦:我不希望他们为每种BoudingVolume类型实现一个功能因此,如果我有一个边界框和一个边界球,那么球形类和框类都应实现

  • isCollidingWith(BoundingBox)
  • isCollidingWith(BoundingSphere)

如果我然后创建一个新的包围体BoundingCylinder(从基类派生),我想直到的BoundingBox和BoundingSphere所实施了编译器将抛出一个错误,isCollidingWith新的功能Cylinder型(和OFC,直到Cylinder已经实现了isCollidingWithBoxSphereCylinder

我不确定如何实现此目标,但我考虑过使用CRTP。这有可能吗?

代词

可以使用CRTP来编造这样的东西

class BoundingBox;
class BoundingSphere;

class Shape
{
    public:
        virtual bool isIntersecting(const BoundingBox&) const = 0;
        virtual bool isIntersecting(const BoundingSphere&) const = 0;
};

class BoundingVolumeBase
{
    public:
        virtual bool checkIntersection(const Shape&) const = 0;
        virtual ~BoundingVolumeBase();
};

template<class Derived>
class BoundingVolume : public BoundingVolumeBase
{
        bool checkIntersection(const Shape& shape) const override
        {
            return shape.isIntersecting (static_cast<const Derived&>(*this));
        }
};

class BoundingBox : public BoundingVolume<BoundingBox> {
    // ...
};

class BoundingSphere : public BoundingVolume<BoundingSphere> {
    // ...
};

现在,如果我们发明了一种新的方法BoundingVolume,直到将新函数添加到它之前,它才能编译Shape

class BoundingCylinder : public BoundingVolume<BoundingCylinder> {
    // ...
};

BoundingCylinder bc; // <-- this will not compile

不必这样做。将虚拟函数用作唯一的基于类型的分派的任何方法都可以使用(无论如何,您最终可能会得到与上述大致等同的结果)。如果您依赖typeid依赖自定义类型标识符,则可能会遇到问题。

这种方法的缺点是类Shape所有具体类型的相互依存BoundingVolume

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章