c ++虚函数
Virtual Function is a function in base class, which is overrided in the derived class, and which tells the compiler to perform Late Binding on this function.
虚函数是基类中的一个函数,该基类在派生类中被覆盖,它告诉编译器对该函数执行后期绑定 。
Virtual
Keyword is used to make a member function of the base class Virtual.
Virtual
关键字用于使基类Virtual的成员函数。
C ++中的后期绑定 (Late Binding in C++)
In Late Binding function call is resolved at runtime. Hence, now compiler determines the type of object at runtime, and then binds the function call. Late Binding is also called Dynamic Binding or Runtime Binding.
在后期绑定功能调用在运行时解决。 因此,现在编译器在运行时确定对象的类型,然后绑定函数调用。 后期绑定也称为动态绑定或运行时绑定。
没有虚拟关键字的问题 (Problem without Virtual Keyword)
Let's try to understand what is the issue that virtual
keyword fixes,
让我们尝试了解virtual
关键字可以解决的问题,
class Base
{
public:
void show()
{
cout << "Base class";
}
};
class Derived:public Base
{
public:
void show()
{
cout << "Derived Class";
}
}
int main()
{
Base* b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show(); //Early Binding Ocuurs
}
Base class
基类
When we use Base class's pointer to hold Derived class's object, base class pointer or reference will always call the base version of the function
当我们使用基类的指针保存派生类的对象时,基类指针或引用将始终调用该函数的基版本
在C ++中使用虚拟关键字 (Using Virtual Keyword in C++)
We can make base class's methods virtual by using virtual keyword while declaring them. Virtual keyword will lead to Late Binding of that method.
我们可以在声明基类的方法时使用virtual关键字使它们虚拟化 。 虚拟关键字将导致该方法的后期绑定。
class Base
{
public:
virtual void show()
{
cout << "Base class\n";
}
};
class Derived:public Base
{
public:
void show()
{
cout << "Derived Class";
}
}
int main()
{
Base* b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show(); //Late Binding Ocuurs
}
Derived class
派生类
On using Virtual keyword with Base class's function, Late Binding takes place and the derived version of function will be called, because base class pointer pointes to Derived class object.
在基类的功能上使用Virtual关键字时,将发生后期绑定,并且将调用该函数的派生版本,因为基类指针指向派生类对象。
使用虚拟关键字并访问派生类的私有方法 (Using Virtual Keyword and Accessing Private Method of Derived class)
We can call private function of derived class from the base class pointer with the help of virtual keyword. Compiler checks for access specifier only at compile time. So at run time when late binding occurs it does not check whether we are calling the private function or public function.
我们可以借助virtual关键字从基类指针调用派生类的私有函数。 编译器仅在编译时检查访问说明符。 因此,在运行时发生后期绑定时,它不会检查我们是在调用私有函数还是公共函数。
#include <iostream>
using namespace std;
class A
{
public:
virtual void show()
{
cout << "Base class\n";
}
};
class B: public A
{
private:
virtual void show()
{
cout << "Derived class\n";
}
};
int main()
{
A *a;
B b;
a = &b;
a->show();
}
Derived class
派生类
C ++中的后期绑定机制 (Mechanism of Late Binding in C++)

To accomplich late binding, Compiler creates VTABLEs, for each class with virtual function. The address of virtual functions is inserted into these tables. Whenever an object of such class is created the compiler secretly inserts a pointer called vpointer, pointing to VTABLE for that object. Hence when function is called, compiler is able to resovle the call by binding the correct function using the vpointer.
为了完成后期绑定,Compiler为具有虚拟功能的每个类创建VTABLEs 。 虚拟函数的地址将插入这些表中。 每当创建此类对象时,编译器都会秘密插入一个名为vpointer的指针,该指针指向该对象的VTABLE。 因此,在调用函数时,编译器能够通过使用vpointer绑定正确的函数来重新整理调用。
要记住的要点 (Important Points to Remember)
Only the Base class Method's declaration needs the Virtual Keyword, not the definition.
仅基类方法的声明需要虚拟关键字,而不需要定义。
If a function is declared as virtual in the base class, it will be virtual in all its derived classes.
如果在基类中将函数声明为虚函数,则该函数在其所有派生类中均为虚函数。
The address of the virtual Function is placed in the VTABLE and the copiler uses VPTR(vpointer) to point to the Virtual Function.
虚拟功能的地址放置在VTABLE中 ,并且编译器使用VPTR (vpointer)指向虚拟功能。
c ++虚函数