简体   繁体   English

C ++中的虚函数实例化之间有什么区别?

[英]What's the difference between virtual function instantiations in C++?

What's the difference between the following two declarations? 以下两个声明之间有什么区别?

virtual void calculateBase() = 0;  
virtual void calculateBase();

I read the first one (=0) is a "pure abstract function" but what does that make the second one? 我读到第一个(= 0)是“纯抽象函数”,但是第二个是什么呢?

The first one is a "pure virtual" - it will make the class abstract, attempting to instantiate it will result in compiler errors. 第一个是“纯虚拟”-它将使类抽象化,尝试实例化它将导致编译器错误。 It is meant to be used as a base class where the derived class implements the necessary behaviour by implementing the pure virtual function. 它旨在用作基类,其中派生类通过实现纯虚函数来实现必要的行为。 You do not have to implement the function in the base class, though you can. 尽管可以,但是不必在基类中实现该功能。
This is a pattern often used for two design patterns: 这是一种经常用于两种设计模式的模式:

  • The "template method" design pattern, where the base class implements a structure around a function call, but the details of the function call must be filled in by the derived class, and “模板方法”设计模式,其中基类实现了围绕函数调用的结构,但是函数调用的详细信息必须由派生类填充,并且
  • The "interface" design pattern, because C++ doesn't have the interface keyword. “接口”设计模式,因为C ++没有interface关键字。 Abstract base classes, ideally with only pure virtual functions and no member data, are the C++ way of defining interfaces. 理想情况下,仅具有纯虚函数且没有成员数据的抽象基类是定义接口的C ++方法。

The second declaration is just an ordinary virtual member function declaration. 第二个声明只是一个普通的虚拟成员函数声明。 You will get compiler errors if you fail to implement the member function in the base class. 如果您无法在基类中实现成员函数,则会出现编译器错误。 It is still virtual which implies that it may be useful to override the behaviour in a derived class. 它仍然是虚拟的,这意味着重写派生类中的行为可能很有用。

First one is called a pure virtual function. 第一个称为纯虚函数。 Normally pure virtual functions will not have any implementation and you can not create a instance of a class containing a pure virtual function. 通常,纯虚函数不会有任何实现,并且您无法创建包含纯虚函数的类的实例。

Second one is a virtual function (ie a 'normal' virtual function). 第二个是虚拟功能(即“正常”虚拟功能)。 A class provides the implementation for this function, but its derived class can override this implementation by providing its own implementation for this method. 类提供了此功能的实现,但是其派生类可以通过为此方法提供自己的实现来覆盖此实现。

The first one doesn't have to be implemented in the base class, but enforces it to be implemented in inherited classes. 第一个不必在基类中实现,但是可以强制它在继承的类中实现。

You have to implement the second one in the base class and it can be implemented in inherited classes. 您必须在基类中实现第二个,并且可以在继承的类中实现。

Basically, when inheriting, you are compelled to override the first one, and are allowed to override the second one. 基本上,在继承时,您不得不覆盖第一个,而被允许覆盖第二个。

Coming from Java, don't you? 来自Java,不是吗?

I think you are mixing terms... 我想你在混几个字...

virtual void x() = 0;

is a pure virtual function , or abstract function. 是一个纯虚函数或抽象函数。 It is a virtual function without implementation. 它是一个虚拟函数,没有实现。 You talk about a pure abstract class, which is the c++ equivalent of an interface, about a class having only abstract functions. 您谈论的是纯抽象类,即与接口等效的c ++,是关于仅具有抽象功能的类。

virtual void x();

is a virtual function, meaning it can be redefined in derived classes, but it's not abstract, so you must provide an implementation for it. 是一个虚函数,意味着可以在派生类中重新定义它,但它不是抽象的,因此必须为其提供实现。

virtual void calculateBase() = 0; 虚无效的calculateBase()= 0;

The first function is Pure virtual function where the implementation can not be done in the class and it act as pure abstract class or Interface class. 第一个函数是Pure虚函数,其中无法在类中完成实现,它充当纯抽象类或Interface类。 The concrete implementation should be done or overriden in subclass. 具体实现应在子类中完成或重写。 It act as interface class to its derived classes. 它充当其派生类的接口类。

virtual void calculateBase(); 虚无效的calculateBase();

This function is virtual function where the implementation (default) can be done in the base class. 此函数是虚拟函数,可以在基类中完成实现(默认)。 But, the derived class must be overriden its own imeplementation. 但是,派生类必须重写其自身的实现。

The second function must have an implementation in the class that declare it (lack of '= 0'), and can be overriden by subClasses. 第二个函数必须在声明它的类中具有一个实现(缺少'= 0'),并且可以被子类覆盖。

The first function may or not have an implementation in the class that declare it, and has to be implemented by subClasses 第一个函数在声明它的类中可能有也可能没有实现,并且必须由子类实现

In Java 在Java中

virtual void calculateBase() = 0;  

would be 将会

abstract void calculateBase();

and

virtual void calculateBase();

would be 将会

void calculateBase();

To be perfectly clear, 十分清楚

void calculateBase();

in C++, is "the same" as 在C ++中,与

final void calculateBase();

in Java. 在Java中。 That is, final is "default" in C++. 也就是说,final是C ++中的“默认”。 There is an exception to this rule however. 但是,此规则有一个例外。 When subclassing a class with a virtual method and reimplementing it without using the virtual keyword, the method in the subclass will not be final. 当使用虚拟方法对类进行子类化并在不使用virtual关键字的情况下重新实现它时,子类中的方法将不是最终方法。

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

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