C++ Exceptional 类的重载、重写(覆盖)和隐藏

通过下面这个例子可以很好的理解重载、重写(覆盖)和隐藏的关系:

class Base
{
public:
    virtual ~Base() {}
    virtual void f(int);
    virtual void f(double);
    virtual void g(int i = 10);
};
void Base::f(int)
{
    cout << "Base::f(int)" << endl;
}

void Base::f(double)
{
    cout << "Base::f(double)" << endl;
}

void Base::g(int i)
{
    cout << "Base::g(int)" << i << endl;
}

class Derived : public Base
{
public:
    void f(complex<double>);
    void g(int i = 20);
};

void Derived::f( complex<double> )
{
    cout << "Derived::f(complex)" << endl;
}

void Derived::g(int i)
{
    cout << "Derived::g()" << i << endl;
}

int main(int argc, char * argv[])
{
    Base b;
    Derived d;
    Base* pb = new Derived;
    b.f(1.0);
    d.f(1.0);
    pb->f(1.0);
    b.g();
    d.g();
    pb->g();
    delete pb;
}

下面分别解释以上程序的输出:
1. b.f(1.0) Base::f(double)
2. d.f(1.0) Derivedf(complex)
3. pb->f(1.0) Base::f(double)
4. b.g() Base::g(int) 10
5. d.g() Derived::g(int) 20
6. pb->g() Derived::g(int) 10

重载: Base中的f(int)f(double) 为重载关系,重载为在同一个类中,具有相同的函数名,但函数参数不同。

重写(覆盖): Derived中的g(int i = 20) 重写了基类Base中 g(int i = 10),但这里要注意的是,当用一个指向子类的基类指针调用g(int) 时,默认的参数是静态绑定的,也就是基类Base中的 i = 10 是静态绑定的,所以 6 才会输出 Derived::g(int) 10

隐藏: Derived中的f( complex ) 隐藏了子类中的同名函数。注意这里并不是重写,因为他们的参数不同。但子类中的函数会隐藏父类中的同名函数。所以 2 3 的输出分别如上。pb->f(1.0)并不会调用Derived中的f(complex) 函数,因为这两者这件的关系并不是重写,而是隐藏,所以调用的仍然是Base中的函数。

你可能感兴趣的:(C++)