虚函数

TIP

本笔记尚未修改

  • 虚函数是实现动态绑定的函数
  • 不要重新定义继承而来的非虚函数
  • 通过虚函数实现运行时多态,虚函数是动态绑定的函数,用virtual关键字说明
  • 加了virtual的函数体都要在类外去实现,不能在类内写成内联函数
  • 虚函数必须是非静态的成员函数
#include <iostream>
using namespace std;

class Base1 {
public:
    virtual void display() const;  //虚函数
};
void Base1::display() const {
    cout << "Base1::display()" << endl;
}

class Base2::public Base1 { 
public:
     virtual void display() const;
};
void Base2::display() const {
    cout << "Base2::display()" << endl;
}
class Derived: public Base2 {
public:
     virtual void display() const; 
};
void Derived::display() const {
    cout << "Derived::display()" << endl;
}

void fun(Base1 *ptr) { 
    ptr->display(); 
}

int main() {    
    Base1 base1;
    Base2 base2;
    Derived derived;    
    fun(&base1);
    fun(&base2);
    fun(&derived);
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

虚析构函数

需要通过基类指针删除派生类对象时,使用虚析构函数,将基类和派生类的析构函数设置为虚析构函数,否则执行delete的结果是不确定的。

#include <iostream>
using namespace std;
class Base
{
public:
    virtual ~Base();
};

//在类外定义虚函数
Base::~Base()
{
    cout << "Base destructor" << endl;
}

class Derived : public Base
{
public:
    Derived();
    virtual ~Derived();

private:
    int *p;
};

Derived::Derived(){
    p = new int(0);
}

//在类外定义虚函数
Derived::~Derived(){
    cout<<"Derived destructor"<<endl;
    delete p;
}

void fun(Base* b){
    delete b;
}

int main(){
    Base *b = new Derived();
    fun(b);
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

虚表virtual table

  • 每个多态类有一个虚表virtual table
  • 虚表中有当前类的各个虚函数的入口地址
  • 每个多态类对象有一个指向当前类的虚表的指针(虚指针vptr)

动态绑定的实现

  1. 构造函数中为对象的虚指针赋值
  2. 通过多态类型的指针或引用调用成员函数时,通过虚指针找到虚表,进而找到所调用的虚函数的入口地址
  3. 通过该入口地址调用虚函数
class Base{
    public:
        virtual void f();
        virtual void g();
    private:
        int i;
};

class Derived: public Base{
    public:
        virtual void f(); //覆盖Base类中的f()
        virtual void h();
    private:
        int j;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

纯虚函数与抽象类

  • 纯虚函数:在基类中声明的虚函数,在该基类中没有定义具体的操作内容,要求各派生类根据实际需要定义自己的版本。
  • 抽象类:可以规范整个类家族对外的统一接口。
#include <iostream>
using namespace std;

//抽象类 不能定义对象
class Base1 { 
public:
    virtual void display() const = 0;   //纯虚函数
};

class Base2: public Base1 { 
public:
    virtual void display() const; //覆盖基类的虚函数
};
void Base2::display() const {
    cout << "Base2::display()" << endl;
}

class Derived: public Base2 { 
public:
     virtual void display() const; //覆盖基类的虚函数
};
void Derived::display() const {
    cout << "Derived::display()" << endl;
} 

void fun(Base1 *ptr) { //可以定义Base1的指针
    ptr->display(); 
}

int main() {    
    Base2 base2;    
    Derived derived;    
    fun(&base2);    
    fun(&derived);  
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

Override & Final (C++11)

  • 多态性的实现:要求派生类的虚函数签名与基类的完全一致。
  • 函数签名:函数名、参数列表、const
  • override:在派生类中,使用override声明的虚函数时,编译器会检查基类是否存在同样标签的虚函数。若不存在,会报错。
  • final:不能被继承和覆盖。

Override

  • override关键字告诉编译器,该函数应该覆盖基类中的函数。如果该函数实际上没有覆盖任何虚函数,则会导致编译器错误。
#include <iostream>
using namespace std;

//抽象类包含纯虚函数
class Exception
{
public:
    //纯虚函数 不存在函数体
    virtual void error_message() = 0;
};

class OutOfMemory : public Exception
{
public:
    virtual void error_message() override;
};

void OutOfMemory::error_message()
{
    cout << "ran out of memeory" << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Final

struct Base1 final
{
};

struct Derived1 : Base1
{
}; // 编译错误:Base1为final,不允许被继承

struct Base2
{
    virtual void f() final;
};

struct Derived2 : Base2
{
    void f(); // 编译错误:Base2::f 为final,不允许被覆盖 
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Last Updated: 2021/11/22 03:17:18
Contributors: oddnaveed