动态重载与静态重载

了解一个概念:动态绑定和静态绑定:

下面看一段代码:

#include <iostream>

using namespace std;

class Parent
{
public:
    void virtual foo() {
        cout << "foo from parent!\n";
    }
    void foo1() {
        cout << "foo1 from parent!\n";
    }
};

class Son : public Parent
{
public:
    void foo() {
        cout << "foo from son!\n";
    }
    void foo1() {
        cout << "foo1 from son!\n";
    }
};

int main()
{
    Parent *p = new Son;
    p->foo();
    p->foo1();
    return 0;

}

 

运行之后,结果是:

foo from son!
foo1 from parent!

这是因为,编译器在遇到irtual函数时,采用动态绑定,这么讲吧,

对于重载的virtual函数,调用哪个函数是根据对象来决定的,这是在函数运行的动态过程中完成的。

对于非virtual函数,调用哪个函数是根据指针类型来决定的:编译器根据指针的类型静态的决定出来,这是静态绑定。

在C++中,函数默认的是静态绑定。

 

下面看一个经典用法:

// dscombine_poly.h
#include <iostream>
#include <vector>

// 公共抽象基类Vehicle
class Vehicle
{
    public:
    virtual void run() const = 0;
};

// 派生于Vehicle的具体类Car
class Car: public Vehicle
{
public:
    virtual void run() const
    {
        std::cout << "run a car/n";
    }
};

// 派生于Vehicle的具体类Airplane
class Airplane: public Vehicle
{
public:
    virtual void run() const
    {
        std::cout << "run a airplane/n";
    }
 
    void add_oil() const
    {
        std::cout << "add oil to airplane/n";
    }
};

// 派生于Vehicle的具体类Airship
class Airship: public Vehicle
{
public:
    virtual void run() const
    {
        std::cout << "run a airship/n";
    }
 
    void add_oil() const
    {
        std::cout << "add oil to airship/n";
    }
};


 
// dscombine_poly.cpp

#include <iostream>
#include <vector>
//#include "dscombine_poly.h"

// run异质vehicles集合
void run_vehicles(const std::vector<Vehicle*>& vehicles)//指针类型是vehicle,如果非virtual函数,则根据
//指针类型来决定调用哪个函数,但此时是virtual的,所以动态绑定,根据调用对象来抉择
{
    for (unsigned int i = 0; i < vehicles.size(); ++i)
    {
        vehicles[i]->run();                 // 根据具体的vehicle类型调用对应的run()
    }
}

// 为某种特定的aircrafts同质对象集合进行“空中加油”
template <typename Aircraft>//模板,定义一个Aircraft类型,做为函数参数类型的集合,那么函数参数中,具体的就可以是任何类型
void add_oil_to_aircrafts_in_the_sky(const std::vector<Aircraft>& aircrafts)
{//这个不管是irtual还是非irtual,都是使用对象类型(指针类型)来选择
    for (unsigned int i = 0; i < aircrafts.size(); ++i)
    {
        aircrafts[i].add_oil();
    }
}

int main()
{
    Car car1, car2;
    Airplane airplane1, airplane2;

    Airship airship1, airship2;
    std::vector<Vehicle*> v;                // 异质vehicles集合
    v.push_back(&car1);
    v.push_back(&airplane1);
    v.push_back(&airship1);
    run_vehicles(v);                        // run不同种类的vehicles

    std::vector<Airplane> vp;               // 同质airplanes集合
    vp.push_back(airplane1);
    vp.push_back(airplane2);
    add_oil_to_aircrafts_in_the_sky(vp);    // 为airplanes进行“空中加油”

    std::vector<Airship> vs;                // 同质airships集合
    vs.push_back(airship1);
    vs.push_back(airship2);
    add_oil_to_aircrafts_in_the_sky(vs);    // 为airships进行“空中加油”
} 

 

 

你可能感兴趣的:(动态重载与静态重载)