多态 *笔记1

实现多态:虚函数

多态的本质:

形式上,使用统一的父类指针做一般性处理,

但是实际执行时,这个指针可能指向子类对象,

形式上,原本调用父类的方法,但是实际上会调用子类的同名方法。

 

【注意】

程序执行时,父类指针指向父类对象,或子类对象时,在形式上是无法分辨的!

只有通过多态机制,才能执行真正对应的方法。

 

基础:虚函数的使用

虚函数的定义:

在函数的返回类型之前使用virtual

只在成员函数的声明中添加virtual, 在成员函数的实现中不要加virtual

 

虚函数的继承:

l  如果某个成员函数被声明为虚函数,那么它的子类【派生类】,以及子类的子类中,所继承的这个成员函数,也自动是虚函数。

如果在子类中重写这个虚函数,可以不用再写virtual, 但是仍建议写virtual, 更可读!

 

进阶1:虚函数的原理-虚函数表

 

单个类的虚函数表

#include <iostream>

using namespace std;

 

class Father {

public:

    virtual void func1() { cout << "Father::func1" << endl; }

    virtual void func2() { cout << "Father::func2" << endl; }

    virtual void func3() { cout << "Father::func3" << endl; }

    void func4() { cout << "非虚函数:Father::func4" << endl; }

public:  //为了便于测试,特别该用public

    int x = 100;

    int y = 200;

    static int z;

};

 

typedef void (*func_t)(void);

int Father::z = 1;

int main(void) {

    Father father;

 

    // 含有虚函数的对象的内存中,最先存储的就是“虚函数表”

    cout << "对象地址:" << (int*)&father << endl;

 

    int* vptr = (int*)*(int*)&father;

    cout << "虚函数表指针vptr:" << vptr << endl;

 

    cout << "调用第1个虚函数: ";

    ((func_t) * (vptr + 0))();

 

    cout << "调用第2个虚函数:";

    ((func_t) * (vptr + 1))();

 

    cout << "调用第3个虚函数: ";

    ((func_t) * (vptr + 2))();

 

   

    cout << "第1个数据成员的地址: " << endl;

    cout <<  &father.x << endl;

    cout << std::hex << (int)&father + 4 << endl;

    cout << "第1个数据成员的值:" << endl;

    cout << std::dec <<  father.x << endl;

    cout << *(int*)((int)&father + 4) << endl;

 

    cout << "第2个数据成员的地址: " << endl;

    cout << &father.y << endl;

    cout << std::hex << (int)&father + 8 << endl;

    cout << "第2个数据成员的值:" << endl;

    cout << std::dec << father.y << endl;

    cout << *(int*)((int)&father + 8) << endl;

 

    cout << "sizeof(father)==" << sizeof(father) << endl;

 

    Father father2;

    cout << "father的虚函数表:";

    cout << *(int*)(*(int*)&father) << endl;

    cout << "father2的虚函数表:";

    cout << *(int*)(*(int*)&father2) << endl;

 

    system("pause");

    return 0;

}

 

执行效果:

 

VS的对象内存分布分析:

项目的命令行配置中添加: /d1 reportSingleClassLayoutFather

 

 

手绘内存分布:

 

对象内,首先存储的是“虚函数表指针”,又称“虚表指针”。

然后再存储非静态数据成员。

 

对象的非虚函数,保存在类的代码中!

对象的内存,只存储虚函数表和数据成员

(类的静态数据成员,保存在数据区中,和对象是分开存储的)

 

添加虚函数后,对象的内存空间不变!仅虚函数表中添加条目

多个对象,共享同一个虚函数表!

 

使用继承的虚函数表

Demo.cpp

#include <iostream>

using namespace std;

 

class Father {

public:

    virtual void func1() { cout << "Father::func1" << endl; }

    virtual void func2() { cout << "Father::func2" << endl; }

    virtual void func3() { cout << "Father::func3" << endl; }

    void func4() { cout << "非虚函数:Father::func4" << endl; }

public:  //为了便于测试,特别该用public

    int x = 100;

    int y = 200;

};

 

class Son : public Father {

public:

    void func1() { cout << "Son::func1" << endl; }

    virtual void func5() { cout << "Son::func5" << endl; }

};

 

typedef void (*func_t)(void);

 

int main(void) {

    Father father;

    Son  son;

 

    // 含有虚函数的对象的内存中,最先存储的就是“虚函数表”

    cout << "son对象地址:" << (int*)&son << endl;

 

    int* vptr = (int*)*(int*)&son;

    cout << "虚函数表指针vptr:" << vptr << endl;

 

    for (int i = 0; i < 4; i++) {

         cout << "调用第" << i + 1 << "个虚函数:";

         ((func_t) * (vptr + i))();

    }

   

    for (int i = 0; i < 2; i++) {

         // +4 是因为先存储了虚表指针

         cout << *(int*)((int)&son + 4 + i * 4) << endl;

    }

 

    system("pause");

    return 0;

}

 

执行效果:

 

内存分布:

 

对象内,首先存储的是“虚函数表指针”,又称“虚表指针”。

然后再存储非静态数据成员。

 

对象的非虚函数,保存在类的代码中!

对象的内存,只存储虚函数表和数据成员

(类的静态数据成员,保存在数据区中,和对象是分开存储的)

 

添加虚函数后,对象的内存空间不变!仅虚函数表中添加条目

多个对象,共享同一个虚函数表!

 

多重继承的虚函数表

#include <iostream>

 

using namespace std;

 

class Father {

public:

    virtual void func1() { cout << "Father::func1" << endl; }

    virtual void func2() { cout << "Father::func2" << endl; }

    virtual void func3() { cout << "Father::func3" << endl; }

    void func4() { cout << "非虚函数:Father::func4" << endl; }

public:

    int x = 200;

    int y = 300;

    static int z;

};

 

class Mother {

public:

    virtual void handle1() { cout << "Mother::handle1" << endl; }

    virtual void handle2() { cout << "Mother::handle2" << endl; }

    virtual void handle3() { cout << "Mother::handle3" << endl; }

public: //为了便于测试,使用public权限

    int m = 400;

    int n = 500;

};

 

class Son : public Father, public Mother {

public:

    void func1() { cout << "Son::func1" << endl; }

    virtual void handle1() { cout << "Son::handle1" << endl; }

    virtual void func5() { cout << "Son::func5" << endl; }

};

 

int Father::z = 0;

 

typedef void(*func_t)(void);

 

int main(void) {

    Son son;

    int* vptr = (int*) * (int*)&son;

    cout << "第一个虚函数表指针:" << vptr << endl;

 

    for (int i = 0; i < 4; i++) {

        cout << "调用第" << i + 1 << "个虚函数:";

         ((func_t) * (vptr + i))();

    }

 

    for (int i = 0; i < 2; i++) {

         cout << *(int*)((int)&son + 4 + i * 4) << endl;

    }

 

    int* vptr2 = (int*) * ((int*)&son + 3);

    for (int i = 0; i < 3; i++) {

         cout << "调用第" << i + 1 << "个虚函数:";

         ((func_t) * (vptr2 + i))();

    }

 

    for (int i = 0; i < 2; i++) {

         cout << *(int*)((int)&son + 16 + i * 4) << endl;

    }

 

    system("pause");

    return 0;

}

 

执行结果

 

内存分布

 

 

 

进阶2:final

用来修饰类,让该类不能被继承

理解:使得该类终结!

class XiaoMi {

public:

    XiaoMi(){}

};

 

class XiaoMi2 final : public XiaoMi  {

    XiaoMi2(){}

};

 

class XiaoMi3 : public XiaoMi2 {  //不能把XiaoMi2作为基类

 

};

 

用来修饰类的虚函数,使得该虚函数在子类中,不能被重写

理解:使得该功能终结!

class XiaoMi {

public:

    virtual void func() final;

};

 

void XiaoMi::func() { //不需要再写final

    cout << "XiaoMi::func" << endl;

}

 

class XiaoMi2 : public XiaoMi  {

public:

    void func() {}; // 错误!不能重写func函数

};

 

进阶3:override

override仅能用于修饰虚函数。

作用:

1. 提示程序的阅读者,这个函数是重写父类的功能。

2. 防止程序员在重写父类的函数时,把函数名写错。

#include <iostream>

using namespace std;

 

class XiaoMi {

public:

    virtual void func() { cout << "XiaoMi::func" << endl; };

};

 

class XiaoMi2 : public XiaoMi  {

public:

    void func() override {}

    //void func() override;  告诉程序员func是重写父类的虚函数

    //void func1() override{} 错误!因为父类没有func1这个虚函数

};

 

int main(void) {

    XiaoMi2 xiaomi;

    return 0;

}

 

override只需在函数声明中使用,不需要在函数的实现中使用。

遗失的子类析构函数

 

为了防止内存泄露,最好是在基类析构函数上添加virtual关键字,使基类析构函数为虚函数

 

目的在于,当使用delete释放基类指针时,会实现动态的析构:

 

如果基类指针指向的是基类对象,那么只调用基类的析构函数

 

如果基类指针指向的是子类对象,那么先调用子类的析构函数,再调用父类的析构函数

 

posted @ 2020-06-28 08:20  CollisionDimension  阅读(88)  评论(0)    收藏  举报