虚函数问题请教
请教一下,虚函数,即在函数前加virtual;当被定为虚函数的时候,到底发生了什么?或者说处理器需要额外做了哪些工作?
比如下面程序:
#include<iostream>
using namespace std;
class Vehicle
{
public:
Vehicle(float speed,int total)
{
Vehicle::speed=speed;
Vehicle::total=total;
}
virtual void ShowMember()
{
cout<<speed<<"|"<<total<<endl;
}
virtual ~Vehicle()
{
cout<<"载入Vehicle基类析构函数"<<endl;
}
protected:
float speed;
int total;
};
class Car:public Vehicle
{
public:
Car(int aird,float speed,int total):Vehicle(speed,total)
{
Car::aird=aird;
}
void ShowMember()
{
cout<<speed<<"|"<<total<<"|"<<aird<<endl;
}
virtual ~Car()
{
cout<<"载入Car派生类析构函数"<<endl;
}
protected:
int aird;
};
void test(Vehicle &temp)
{
temp.ShowMember();
}
void DelPN(Vehicle *temp)
{
delete temp;
}
void test25()
{
Car* a=new Car(100,1,1);
a->ShowMember();
DelPN(a);
}