如果一个函数被定义为虚函数,它究竟意味着什么?它与纯虚函数是相同的吗?


当前回答

来自维基百科的虚拟功能 ...

在面向对象编程中,在c++和Object Pascal等语言中,虚函数或虚方法是一种可继承和可覆盖的函数或方法,可以方便地进行动态分派。这个概念是面向对象编程(OOP)的(运行时)多态性部分的重要组成部分。简而言之,虚函数定义了要执行的目标函数,但在编译时可能不知道目标。

与非虚函数不同,当虚函数被重写时,派生最多的版本将用于类层次结构的所有级别,而不仅仅是创建它的级别。因此,如果基类的一个方法调用虚方法,将使用派生类中定义的版本,而不是基类中定义的版本。

这与非虚函数相反,非虚函数仍然可以在派生类中被重写,但“新”版本只会被派生类及其以下类使用,但根本不会改变基类的功能。

而. .

纯虚函数或纯虚方法是一种虚函数,如果派生类不是抽象的,则需要由派生类实现。

当纯虚方法存在时,类是“抽象的”,不能单独实例化。相反,必须使用实现纯虚方法的派生类。纯虚类根本没有在基类中定义,因此派生类必须定义它,否则派生类也是抽象的,不能实例化。只有没有抽象方法的类才能被实例化。

虚类提供了一种重写基类功能的方法,而纯虚类则需要这种方法。

其他回答

I'd like to comment on Wikipedia's definition of virtual, as repeated by several here. [At the time this answer was written,] Wikipedia defined a virtual method as one that can be overridden in subclasses. [Fortunately, Wikipedia has been edited since, and it now explains this correctly.] That is incorrect: any method, not just virtual ones, can be overridden in subclasses. What virtual does is to give you polymorphism, that is, the ability to select at run-time the most-derived override of a method.

考虑下面的代码:

#include <iostream>
using namespace std;

class Base {
public:
    void NonVirtual() {
        cout << "Base NonVirtual called.\n";
    }
    virtual void Virtual() {
        cout << "Base Virtual called.\n";
    }
};
class Derived : public Base {
public:
    void NonVirtual() {
        cout << "Derived NonVirtual called.\n";
    }
    void Virtual() {
        cout << "Derived Virtual called.\n";
    }
};

int main() {
    Base* bBase = new Base();
    Base* bDerived = new Derived();

    bBase->NonVirtual();
    bBase->Virtual();
    bDerived->NonVirtual();
    bDerived->Virtual();
}

这个程序的输出是什么?

Base NonVirtual called.
Base Virtual called.
Base NonVirtual called.
Derived Virtual called.

Derived重写Base的每个方法:不仅是虚方法,而且是非虚方法。

我们看到,当你有一个基类指向派生类的指针(bDerived)时,调用NonVirtual调用基类实现。这是在编译时解析的:编译器看到bDerived是一个Base*,而NonVirtual不是virtual,所以它在类Base上进行解析。

然而,调用Virtual调用派生类实现。由于关键字virtual,方法的选择发生在运行时,而不是编译时。这里在编译时发生的事情是,编译器看到这是一个Base*,并且它正在调用一个虚方法,所以它插入一个对虚表的调用,而不是类Base。此虚表在运行时实例化,因此运行时解析为最派生的覆盖。

我希望这不会让你太困惑。简而言之,任何方法都可以被覆盖,但只有虚方法可以提供多态性,即运行时选择派生最多的覆盖。然而,在实践中,覆盖非虚拟方法被认为是不好的实践,很少被使用,所以很多人(包括撰写维基百科文章的人)认为只有虚拟方法可以被覆盖。

virtual关键字赋予c++支持多态的能力。当你有一个指向某个类的对象的指针时,例如:

class Animal
{
  public:
    virtual int GetNumberOfLegs() = 0;
};

class Duck : public Animal
{
  public:
     int GetNumberOfLegs() { return 2; }
};

class Horse : public Animal
{
  public:
     int GetNumberOfLegs() { return 4; }
};

void SomeFunction(Animal * pAnimal)
{
  cout << pAnimal->GetNumberOfLegs();
}

在这个(愚蠢的)示例中,GetNumberOfLegs()函数根据所调用对象的类返回适当的数字。

现在,考虑函数“SomeFunction”。它不关心传递给它的是什么类型的animal对象,只要它是从animal派生的。编译器会自动地将任何Animal派生类强制转换为Animal,因为它是一个基类。

如果我们这样做:

Duck d;
SomeFunction(&d);

它会输出'2'。如果我们这样做:

Horse h;
SomeFunction(&h);

它会输出'4'。我们不能这样做:

Animal a;
SomeFunction(&a);

因为它不会编译,因为GetNumberOfLegs()虚函数是纯的,这意味着它必须通过派生类(子类)来实现。

纯虚函数主要用于定义:

A)抽象类

这些是基类,你必须从它们中派生出来,然后实现纯虚函数。

b)接口

这些是“空”类,其中所有的函数都是纯虚的,因此你必须派生然后实现所有的函数。

虚函数或虚方法是一种函数或方法,其行为可以在继承类中被具有相同签名的函数覆盖

这并不是对虚函数的一个很好的解释。因为,即使成员不是虚成员,继承类也可以重写它。你可以自己去看看。

当函数以基类作为参数时,这种差异就会显现出来。当您将继承类作为输入时,该函数将使用被覆盖函数的基类实现。但是,如果该函数是虚函数,则使用派生类中实现的函数。

纯虚函数

试试这段代码

#include <iostream>
using namespace std;
class aClassWithPureVirtualFunction
{

public:

    virtual void sayHellow()=0;

};

class anotherClass:aClassWithPureVirtualFunction
{

public:

    void sayHellow()
    {

        cout<<"hellow World";
    }

};
int main()
{
    //aClassWithPureVirtualFunction virtualObject;
    /*
     This not possible to create object of a class that contain pure virtual function
    */
    anotherClass object;
    object.sayHellow();
}

在类anotherClass中,删除sayhello函数并运行代码。你会得到错误!因为当一个类包含一个纯虚函数时,不能从该类创建对象,并且它是继承的,那么它的派生类必须实现该函数。

虚函数

试试其他代码

#include <iostream>
using namespace std;
class aClassWithPureVirtualFunction
{

public:

    virtual void sayHellow()
    {
        cout<<"from base\n";
    }

};

class anotherClass:public aClassWithPureVirtualFunction
{

public:

    void sayHellow()
    {

        cout<<"from derived \n";
    }

};
int main()
{
    aClassWithPureVirtualFunction *baseObject=new aClassWithPureVirtualFunction;
    baseObject->sayHellow();///call base one

    baseObject=new anotherClass;
    baseObject->sayHellow();////call the derived one!

}

这里sayhello函数在基类中被标记为虚函数。它表示编译器尝试在派生类中搜索函数并实现该函数。如果没有找到,则执行基本的一个。谢谢

在c++类中,virtual是一个关键字,它表示一个方法可以被子类重写(即由子类实现)。例如:

class Shape 
{
  public:
    Shape();
    virtual ~Shape();

    std::string getName() // not overridable
    {
      return m_name;
    }

    void setName( const std::string& name ) // not overridable
    {
      m_name = name;
    }

  protected:
    virtual void initShape() // overridable
    {
      setName("Generic Shape");
    }

  private:
    std::string m_name;
};

在这种情况下,子类可以覆盖initShape函数来做一些专门的工作:

class Square : public Shape
{
  public: 
    Square();
    virtual ~Square();

  protected:
    virtual void initShape() // override the Shape::initShape function
    {
      setName("Square");
    }
}

术语纯虚指的是需要由子类实现而基类还没有实现的虚函数。通过使用virtual关键字并在方法声明的末尾添加a =0,可以将一个方法指定为纯虚拟方法。

所以,如果你想让Shape::initShape纯虚拟,你会做以下事情:

class Shape 
{
 ...
    virtual void initShape() = 0; // pure virtual method
 ... 
};

通过向类中添加纯虚方法,可以使类成为抽象基类 这对于将接口与实现分离非常方便。