![](/img/trans.png)
[英]Implementing pure virtual function with common details for derived classes (c++)
[英]c++ Pure virtual functions dependent on derived classes
我正在开发一个边界框/碰撞检测系统,并且正在使用不同类型的边界卷,像所有边界卷一样使用id来派生相同的基类,然后使用纯虚拟函数强制所有派生类实现诸如
isCollidingWith(BoudingBox)
但这给我带来了麻烦:我不希望他们为每种BoudingVolume
类型实现一个函数。 因此,如果我有一个边界框和一个边界球,那么球形类和框类都应实现
isCollidingWith(BoundingBox)
isCollidingWith(BoundingSphere)
如果然后我创建一个类似于BoundingCylinder
的新BoundingVolume(通过从基类派生),则我希望编译器抛出错误,直到BoundingBox和BoundingSphere为新的Cylinder
类型实现了isCollidingWith
函数为止(并且在Cylinder
实现了isCollidingWith
之前为ofc)用于Box
, Sphere
和Cylinder
。
我不确定如何实现此目标,但我考虑过使用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
相互依赖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.