这种情况下也会发生多态。以前不知道。

#include <iostream>

using std::cout;

class A{
public:
    virtual void f(){cout << "A f\n";}
};

class B:public A{
public:
    void f(){cout << "B f\n";}
};

void test(A*p)
{
    (*p).f();
}

int main()
{
    B b;
    test(&b);

    return 0;
}

输出:

猜你喜欢

转载自www.cnblogs.com/buddho/p/11865209.html