虚函数和多态

多态

公有继承派生类对象使用基类的方法,如果希望同一种方法派生类和基类的行为是不同的,也即方法的行为取决于调用该方法的对象,这种行为称为多态。

  • 基类使用虚方法(此时派生类自动成为虚方法,可以指出也可以不指出)。
  • 基类不使用虚方法,在派生类直接重定义该方法。

声明为virtual

如果不将函数声明为virtual,程序将根据引用类型或者指针类型选择方法。如果声明为virtual,程序将根据引用或者指针指向对象本身的类型来选择方法。
举例来说

基类中方法和析构函数均不声明为virtual的情况

#include 
#include 
using namespace std;

class Base
{
public:
    Base() { cout << "created Base\n"; }
    void function() { cout << "this is Base's function" << endl; }
    ~Base(){cout << "this is Base's destroy" << endl;}
};

class Derived : public Base
{
public:
    Derived(){ cout << "created derived" << endl;}
    void function(){cout << "this is Derived's function" << endl;}
    ~Derived(){cout << "this is Derived's destroy" << endl;}
};

int main()
{
    Base* ptr = new Derived;
    // std::unique_ptr ptr(new Derived);

    ptr->function();
    delete ptr; // delete 语句的根本含义在于调用其析构函数,并释放内存

    return 0;
}

输出

created Base
created derived          可以看出构造函数不受影响,被执行了
this is Base's function                虽然对象是派生类,但是由于指针是指向基类的,所以执行了基类的方法
this is Base's destroy                 虽然对象是派生类,但由于指针是指向基类的,所以只执行了基类的析构函数,所以该对象未被完全释放。

基类中方法与析构函数均声明为virtual的情况

#include 
#include 
using namespace std;

class Base
{
public:
    Base() { cout << "created Base\n"; }
    virtual void function() { cout << "this is Base's function" << endl; }
    virtual ~Base(){cout << "this is Base's destroy" << endl;}
};

class Derived : public Base
{
public:
    Derived(){ cout << "created derived" << endl;}
    void function(){cout << "this is Derived's function" << endl;}
    ~Derived(){cout << "this is Derived's destroy" << endl;}
};

int main()
{
    Base* ptr = new Derived;
    // std::unique_ptr ptr(new Derived);

    ptr->function();
    delete ptr; // delete 语句的根本含义在于调用其析构函数,并释放内存

    return 0;
}

输出

created Base
created derived
this is Derived's function      执行的是派生类的方法,虽然指针指向的是基类
this is Derived's destroy       析构函数也被正确调用了,
this is Base's destroy

其他说明

若方法是通过对象(而不是指针或者引用调用的),则没有虚方法特性!

你可能感兴趣的:(c++)