问题描述
我有一个基类, Primitive ,从中导出其他几个类 - Sphere , Plane 等
原始强制执行一些功能, code> intersect(),在其子类上通过纯虚函数。 相交的计算取决于实例数据,因此将其作为成员方法是有意义的。
问题出现在以下:
我想让每个派生实例能够识别其类型,例如通过 std :: string type()成员方法。因为同一个类的所有实例将返回相同的类型,所以使 type() a static 。因为我也希望每个 Primitive 子类实现这个方法,我也想使它成为一个纯虚函数,如 intersect()。
但是,在C ++中不允许使用静态虚方法。
和
提出类似的问题,但它们不包括对派生类强制执行函数的要求。
任何人都可以帮助我你可以有一个非静态的虚拟方法调用静态的(或返回一个静态的字符串),适当的实现方法是:?
#include< iostream>
#include< string>
struct IShape {
virtual const std :: string& type()const = 0;
};
struct Square:virtual public IShape {
virtual const std :: string& type()const {return type_; }
static std :: string type_;
};
std :: string Square :: type _(square);
int main(){
IShape * shape = new Square;
std :: cout<< shape-> type()< \\\
;
}
请注意,您必须实现 type()方法,所以你能做的最好的是字符串是静态的。但是,您可以考虑使用枚举而不是字符串,避免在代码中进行不必要的字符串比较。
现在,回到问题的基本原理,我认为设计有点有缺陷。你不能真正有一个通用的交叉函数对所有类型的形状进行操作,因为交叉形状的类型变化很大,即使对于相同类型的形状(两个平面可以在平面中交叉,也可以不交叉所有,例如)。因此,在尝试提供一个通用的解决方案,你会发现自己在这个地方执行这些类型的检查,这将增加不可避免的更多的形状你添加。
I have a base class, Primitive, from which I derive several other classes--Sphere, Plane, etc.
Primitive enforces some functionality, e.g intersect(), on its subclasses through pure virtual functions. The computation of intersect depends on instance data, so it makes sense to have it as a member method.
My problem arises in the following:I want every derived instance to be able to identify its type, say through a std::string type() member method. As all instances of the same class will return the same type, it makes sense to make type() a static method. As I also want every Primitive subclass to implement this method, I would also like to make it a pure virtual function, like intersect() above.
However, static virtual methods are not allowed in C++.C++ static virtual members?andCan we have a virtual static method ? (c++)ask similar questions but they do not include the requirement of enforcing the function on derived classes.
Can anyone help me with the above?
You could have a non-static virtual method calling the static one (or returning a static string), appropriately implemented in each derived class.
#include <iostream> #include <string> struct IShape { virtual const std::string& type() const =0; }; struct Square : virtual public IShape { virtual const std::string& type() const { return type_; } static std::string type_; }; std::string Square::type_("square"); int main() { IShape* shape = new Square; std::cout << shape->type() << "\n"; }
Note that you will have to implement the type() method for every subclass anyway, so the best you can do is for the string to be static. However, you may consider using an enum instead of a string, do avoid unnecessary string comparisons in your code.
Now, going back to the fundamentals of the problem, I think the design is somewhat flawed. You cannot really have a general intersection function that operates on all kinds of shapes, because the types of shapes resulting from intersections vary greatly, even for the same type of shape (two planes can intersect in a plane, a line, or not intersect at all, for example). So in trying to provide a general solution, you will find yourself performing these kind of type checks all over the place, and this will grow unmaintainably the more shapes you add.
这篇关于如何在派生类上强制使用静态成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!