简体   繁体   English

虚函数和抽象类

[英]virtual functions and abstract classes

I read in my book: 我读了我的书:

An abstract class is a class that is designed to be specifically used as a base class. 抽象类是一个专门用作基类的类。 An abstract class contains at least one pure virtual function. 抽象类包含至少一个纯虚函数。 You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration. 通过在类声明中的虚拟成员函数的声明中使用纯说明符(= 0)来声明纯虚函数。

Is it mandatory for an abstract class to have a virtual function? 抽象类是否必须具有虚函数? Why? 为什么?

What is the difference between pure virtual function and virtual function and what is the need of them? 纯虚函数和虚函数有什么区别?它们的需求是什么?

A pure virtual function specifies an interface that must be overridden in a derived class to be able to create objects of the derived class. 纯虚函数指定必须在派生类中重写的接口,以便能够创建派生类的对象。

A (non-pure) virtual function specifies an interface that can be overridden in a derived class, but the base class provides a default implementation of the interface. (非纯)虚函数指定在派生类中重写的接口,但基类提供接口的默认实现。

For most practical purposes, yes, an abstract base class must contain at least one virtual function. 对于大多数实际目的,是的,抽象基类必须包含至少一个虚函数。 The whole point of an abstract base class is to specify an interface that's implemented by derived classes. 抽象基类的重点是指定由派生类实现的接口。 That interface is specified in terms of a number of virtual functions that can be called. 该接口是根据可以调用的许多虚函数指定的。 Without virtual functions, you haven't specified an interface, which makes it pretty hard for the abstract base class to accomplish much. 没有虚函数,您还没有指定接口,这使得抽象基类很难完成。

If you use an abstract class, that means you don't want to instantiate this class incorrectly. 如果使用抽象类,则意味着您不希望错误地实例化此类。 And you must use pure virtual function in that class.But declaring or writing function in class is your choice. 并且你必须在该类中使用纯虚函数。但是在类中声明或编写函数是你的选择。 You can write the function in derived class too. 您也可以在派生类中编写函数。

不同之处在于您无法实例化抽象类 - 它充当接口

To be abstract a class must have one pure virtual function. 要成为抽象类,必须有一个纯虚函数。 Only virtual function can be pure since it could be overriden and thus it's useful for polymorphism. 只有虚函数可以是纯函数,因为它可以被覆盖,因此它对多态有用。 Pure non-virtual function doesn't make sense because it doesn't do anything and couldn't be overriden, so it's useless, and doesn't exist :) 纯粹的非虚函数没有意义,因为它没有做任何事情而且无法覆盖,所以它没用,并且不存在:)

Yes, it must have at least one pure virtual function. 是的,它必须至少有一个纯虚函数。

In case all the virtual functions for your base class have an implementation, and you would like to make it abstract nonetheless, you can use a pure virtual destructor : 如果你的基类的所有虚函数都有一个实现,并且你想让它抽象,你可以使用纯虚析构函数

class MyAbstractClass
{
    virtual ~MyAbstractClass() = 0;

    virtual void f() 
    {
        IHaveAnImplementation(); 
        SoICannotBePure();
    }
};

// The destructor can unfortunately not be defined inline
MyAbstractClass::~MyAbstractClass() {}

This only a conveniance: a pure destructor is not really a pure function since it has a definition. 这只是一个方便:纯粹的析构函数并不是真正的纯函数,因为它有一个定义。 It is only a marker saying that the class cannot be instantiated, although it has no other abstract functions. 它只是一个标记,表示该类无法实例化,尽管它没有其他抽象函数。

A pure virtual function is one which must be overridden by any concrete (ie, non-abstract) derived class. 纯虚函数是必须由任何具体(即非抽象)派生类重写的函数。 This is indicated in the declaration with the syntax " = 0" in the member function's declaration. 这在成员函数声明中使用语法" = 0"的声明中指出。

Example: 例:

class AbstractClass {
public:
  virtual void AbstractMemberFunction() = 0; // Pure virtual function makes
                                             // this class Abstract class.
  virtual void NonAbstractMemberFunction1(); // Virtual function.

  void NonAbstractMemberFunction2();
};

In general an abstract class is used to define an implementation and is intended to be inherited from by concrete classes. 通常,抽象类用于定义实现,并且旨在从具体类继承。 It's a way of forcing a contract between the class designer and the users of that class. 这是强制类设计者和该类用户之间签订合同的一种方式。 If we wish to create a concrete class ( a class that can be instantiated ) from an abstract class we must declare and define a matching member function for each abstract member function of the base class. 如果我们希望从抽象类创建一个具体类( 可以实例化的类 ),我们必须为基类的每个抽象成员函数声明和定义匹配的成员函数。 Otherwise, if any member function of the base class is left undefined, we will create a new abstract class ( this could be useful sometimes ). 否则,如果基类的任何成员函数未定义,我们将创建一个新的抽象类( 这有时可能很有用 )。

Is it mandatory for an abstract class to have a virtual function? 抽象类是否必须具有虚函数? Why? 为什么?

It depends on the definition you use. 这取决于您使用的定义。 The standard use 标准使用

A class is abstract if it has at least one pure virtual function. 如果一个类至少有一个纯虚函数,则它是抽象的。

so yes it is mandatory. 所以是的,它是强制性的。 Informally you may use another definition but then you risk confusion in a C++ context. 非正式地,您可以使用另一个定义,但在C ++上下文中存在混淆。

What is the difference between pure virtual function and virtual function and what is the need of them? 纯虚函数和虚函数有什么区别?它们的需求是什么?

A pure virtual member: 纯虚拟成员:

  • must be overridden in all non abstract derived class 必须在所有非抽象派生类中重写

  • can be left without definition (but giving a definition is possible, it is even mandatory in the case of a pure virtual destructor). 可以保留没有定义(但是给定义是可能的,在纯虚析构函数的情况下甚至是强制的)。

pure virtual意味着该方法没有实现,因此它强制任何非抽象子类提供该实现。

In C++, the only way to make a class abstract is to put at least one pure virtual function in it. 在C ++中,使类抽象的唯一方法是在其中放置至少一个纯虚函数。 The compiler won't let you instantiate a class that contains a pure virtual function, because then you'd have an object with a function that has no definition. 编译器不允许您实例化包含纯虚函数的类,因为那时您将拥有一个没有定义的函数的对象。 There are probably cryptic ways to get around this, but this is the standard practice. 可能有一些神秘的方法来解决这个问题,但这是标准做法。

The difference between a pure virtual and virtual function is that a pure virtual does not specify the implementation of the method. 纯虚函数和虚函数之间的区别在于纯虚函数不指定方法的实现。 The =0 syntax tells the compiler that the class is not providing a definition for the function, which makes the function pure virtual and makes the class abstract. =0语法告诉编译器该类没有为函数提供定义,这使得函数纯虚拟并使类成为抽象。 Any class deriving from the abstract base class must define the pure virtual function, or else the subclass will be abstract as well. 从抽象基类派生的任何类都必须定义纯虚函数,否则子类也将是抽象的。

A "non-pure" virtual function is one which is marked with the virtual keyword, but a definition for the function is supplied in the base class. “非纯”虚函数是用virtual关键字标记的函数,但函数的定义在基类中提供。 This means that the base class provides an implementation of the function, which any subclasses can override if desired. 这意味着基类提供了函数的实现,如果需要,任何子类都可以覆盖它。 The virtual keyword allows polymorphism to work when you're using base class pointers that point to derived class objects. 当您使用指向派生类对象的基类指针时,virtual关键字允许多态。

A virtual function can be overridden in a derived class. 可以在派生类中重写虚函数。

A pure virtual function must be overridden in a derived class. 必须在派生类中重写纯虚函数。

A class with pure virtual functions cannot be instantiated. 具有纯虚函数的类无法实例化。

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

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