虚函数和多态

多态

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

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

声明为virtual

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

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

#include <iostream>
#include <memory>
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<Base> 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 <iostream>
#include <memory>
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<Base> 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

其他说明

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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值