【耗子尾汁 | 重载、重写、隐藏把我整蒙了~】_重载


1.啥叫重载、重写(覆盖)、隐藏?

重载(overload)同一个可访问区域内被声明的几个具有不同参数列表(参数的类型、个数、顺序不同)的同名函数。根据参数列表,最终确定调用哪个函数,重载不关心函数的返回值类型。示例如下:
class A{
    public:
        void test(int i);  // overload
        void test(double i);  // overload
        void test(int i, double j);  // overload
        void test(double i, int j);  // overload
        // int test(int i);  函数重载不关心返回值的类型,所以不是函数重载!!!
};
重写/覆盖(override)派生类中存在重新定义的函数派生类中重新定义的函数,其函数名、参数列表、返回值类型都必须与基类中被重写的函数完全一样!只有函数体不同,派生类调用时会调用派生类的重写函数,不会调用被重写的基类中的函数。基类中被重写的函数必须有virtual来修饰!示例如下:
/* 重写/覆盖(override) */
class Base{
    public:
        virtual void fun(int i){   // 基类中被重写的函数!
            cout << "Base::fun(int): " << i << endl;  
        }
};

class Derived: public Base{
    public:
        virtual void fun(int i){   // 在派生类中重写基类中的函数
            cout << "Derived::fun(int): " << i << endl;
        }
};

int main(){
    Base b;
    Base *pb = new Derived();
    pb->fun(666);  // Derived::fun(int) 
    return 0;
}
隐藏(hide): 指的是派生类中的函数屏蔽了与其同名的基类中的函数。注意:只要同名函数就行,不管参数列表是否相同,基类中的函数都会被屏蔽。示例如下:
// 隐藏hide
class AA{
    public:
        void func(double i, int j){
            cout << "AA::func(double, int): " << endl;
        }
};

class BB: public AA{
    public:
        int func(int i){
            cout << "BB::func(int i): " << i << endl;
            return i;
        }
};

int main(){
    BB bb;
    bb.func(1000);  // BB::func(int i)
    // bb.func(0.01, 1000);  BB::func函数不接受2个参数!!!
    return 0;
}
  • 重载与重写的区别
    • 范围不同:重写和被重写的函数在不同的类中,重载和被重载的函数在同一个类中。
    • 参数列表不同:重写和被重写的函数参数列表一定相同,重载和被重载的函数参数列表一定不同。
    • virtual关键字使用不同:重写的基类必须要有virtual修饰,重载函数和被重载的函数可以被virtual修饰,也可以没有。
  • 隐藏与重写、重载的区别
    • 与重载的不同:隐藏函数与被隐藏函数在不同的类中。
    • 参数列表不同:隐藏函数和被隐藏函数的参数列表可以相同,也可以不同,但函数名一定相同!当参数不同时,无论基类中的函数是否被virtual修饰,基类函数都是被隐藏,而不是重写。可以把重写理解成隐藏的特殊情况。示例如下:
// 重载 重写 隐藏的对比
class AAA{
    public:
        virtual void f(float x){
            cout << "AAA::f(float x): " << x << endl;
        }
        void g(float x){
            cout << "AAA::g(float x): " << x << endl;
        }
        void h(float x){
            cout << "AAA::h(float x): " << x << endl;
        }
        // 函数重载
         void foo(int i){
            cout << "AAA::foo(int i): " << i << endl;
        }

        void foo(double d){
            cout << "AAA::foo(double d): " << d << endl;
        }
};

class BBB: public AAA{
    public:
        virtual void f(float x){
            cout << "BBB::f(float x): " << x << endl;
        }
        void g(int x){
            cout << "BBB::g(int x): " << x << endl;
        }
        void h(float x){
            cout << "BBB::h(float x): " << x << endl;
        }
};

int main(){
    BBB bbb;
    AAA *paaa = &bbb;
    BBB *pbbb = &bbb;
    paaa->f(3.14);
    pbbb->f(3.14);

    paaa->g(3.14);
    pbbb->g(3.14);

    paaa->h(3.14);
    pbbb->h(3.14);

    paaa->foo(3.14);
    return 0;
}
  • 解释如下:
    • 函数f()是重写/覆盖,派生类BBB中的f()覆盖了基类AAA中的f()
    • 函数g()是隐藏,不是重载!因为不是在同一个类中;又因为函数g()中的参数列表不同,所以也不是重写,同时也没有用virtual进行修饰,虽然出现在两个不同的类中!
    • 函数h()是隐藏,不是重写,因为没有用virtual进行修饰,虽然出现在两个不同的类中!
    • 函数foo()是重载,因为发生在同一个类AAA中,不是隐藏与重写!