简体   繁体   English

具有静态函数的类将成为C ++中的抽象基类

[英]Class with a static function to be made abstract base class in C++

Have a use case where 有一个用例

Class foo {

public:

  static std::string make(std::string a) { .. }

}

I want to make foo an abstract base class but obviously make cannot be in this abstract base since virtual functions cannot be static. 我想将foo设为抽象基类,但显然make不能位于该抽象基类中,因为虚函数不能为静态。

Like this 像这样

Class foo {

public:
   static virtual std::string make (std::string) = 0; //error this cannot be done 

}

Class fooImpl: foo{

public:
   std::string make(std::string a) { ..}
}

Is making the method non static in the abstract class or have derived classes have static methods a good approach from a design perspective. 从设计的角度来看,使方法在抽象类中是非静态的,或者使派生类具有静态方法是一种好方法。

You can make the destructor a pure virtual function: 您可以使析构函数成为纯虚函数:

class foo {
  public:
    virtual ~foo() = 0;
}

You just need to make sure to provide an implementation for the destructor anyway: 您只需要确保为析构函数提供一个实现:

foo::~foo() {}

Edit: 编辑:
Therefore you have a class that can be derived from, but itself cannot be instantiated. 因此,您有一个可以派生但不能实例化的类。

The question, as posted by others is whether the method should be virtual or static. 其他人提出的问题是该方法应为虚拟还是静态。

Polymorphism is based on the idea that you are calling a behavior of the particular object instance regardless of what type of reference you have to it. 多态性是基于这样的想法,即您要调用特定对象实例的行为,而不管对其具有哪种引用类型。

base& somefunction(); // may return base or derived objects
base &b = somefunction();
b.method();

Now, if method behavior changes from base to derived and you want the behavior of the real instance you are referring to, then you need polymorphism and thus a virtual method. 现在,如果方法行为从基本变为派生,并且您想要引用的实际实例的行为,则需要多态性,因此需要虚拟方法。 The fact that the method may or not use member data is irrelevant, the important part is that it is a behavior bound to the particular instance. 该方法可能使用或不使用成员数据的事实无关紧要,重要的部分是它是绑定到特定实例的行为。

On the other end, static methods are bound to the class. 另一方面,静态方法绑定到该类。 That is, it is a behavior of the class you are dealing with not a behavior of the instance being referred. 也就是说,这是您正在处理的类的行为,而不是所引用实例的行为。 As such, even if the syntax above can be used it will call the static method in class base since that is the class that you are dealing with. 因此,即使上面的语法可以使用它将调用类的静态方法 ,因为这是你正在处理的类。

There is no reason to determine that any of the design options you are dealing with is better than the other without a deeper knowledge of the domain. 在没有更深的领域知识的情况下,没有理由确定您要处理的任何一种设计方案都比另一种更好。 I hope the reasoning above helps you make a decision. 希望以上推理能帮助您做出决定。

Wraping the static functions in a non static virtual function allows polymorphism when you have an instance and static calls when you don't (I can't think of any use cases where you'll only need polymorphism when you don't already have an instance). 将静态函数包装在非静态虚拟函数中可以在您拥有实例时实现多态,而在没有实例时进行静态调用(我想不出任何用例,当您没有实例时就只需要多态)实例)。 You loose the compile time error if you forget to put an implmentation of the static virtual in a derived class and it isn't very pretty but not much else you can do. 如果您忘记在派生类中添加静态虚拟的实现,则会释放编译时错误,虽然它不是很漂亮,但是您还可以做很多其他事情。 It is a shame that this oversight is not being fixed in the C0x standard but it is relatively narrow requirement so I guess there isn't the demand 遗憾的是,这种疏忽并未在C0x标准中得到解决,但要求相对狭窄,因此我认为没有需求

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

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