c++多态的实现

在面试中常常会有面试官问道,c++的多态的实现机制。那么,多态到底该如何实现呢?

多态的简单介绍

一般来说,多态分为两种, 静态多态动态多态。静态多态也称编译时多态,主要包括 模板重载。而动态多态则是通过类的继承和虚函数来实现,当基类和子类拥有同名同参同返回的方法,且该方法声明为虚方法,当基类对象,指针,引用指向的是派生类的对象的时候,基类对象,指针,引用在调用基类的虚函数,实际上调用的是派生类函数。这就是动态多态。

静态多态的实现

静态多态靠编译器来实现,简单来说就是编译器对原来的函数名进行修饰,在c语言中,函数无法重载,是因为,c编译器在修饰函数时,只是简单的在函数名前加上下划线”_” ,不过从gcc编译器编译之后发现函数名并不会发生变化。而c++编译器不同,它根据函数参数的类型,个数来对函数名进行修饰,这就使得函数可以重载,同理,模板也是可以实现的,针对不同类型的实参来产生对应的特化的函数,通过增加修饰,使得不同的类型参数的函数得以区分。
以下段程序为例

#include
using namespace std;

template
int fun(T1 t1, T2 t2){}

int foofun(){}
int foofun(int){}
int foofun(int , float){}
int foofun(int , float ,double){}

int main(int argc, char *argv[])
{
    fun(1, 2);
    fun(1, 1.1);
    foofun();
    foofun(1);
    foofun(1, 1.1);
    foofun(1, 1.1, 1.11);
    return 0;
}

经过编译之后:

c++多态的实现

只选取main函数部分来看:

c++多态的实现
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:7d7abeb1-35fc-42ff-9113-0b40cd21ade8
[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:6bf7b908-0996-41a8-8755-16f1ed91294d

动态多态的实现

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:1efbe4e3-e030-42d8-a96a-3be4e1590151

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:3bf7538b-ea89-4d1d-ae41-90ee31062a80

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:1cb5ff57-dcde-43b4-ac0e-fc91bd489bd0

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:04c09b5d-a576-4266-bf93-41253a259fc4

以下段程序为例:

#include

using namespace std;

class Base
{
public:
    virtual void fun()
    {
        cout << "this is base fun" << endl;
    }
};

class Derived : public Base
{
public:
    void fun()
    {
        cout << "this is Derived fun" << endl;
    }
};
int main(int argc, char *argv[])
{
    Base b1;
    Derived d1;
    Base *pb = &d1;
    Derived *pd = (Derived *)&b1;
    b1.fun();
    pd->fun();
    d1.fun();
    pb->fun();
    return 0;
}

运行结果如下:

c++多态的实现
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:2ae85ff8-5be4-4bd2-863d-af3f2bfad9ce
[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:f6e4c0a1-0f5a-4f5b-a46b-01dff5f84eb9

Original: https://www.cnblogs.com/0x12345678/p/5677598.html
Author: Hackergin
Title: c++多态的实现

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/562763/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球