#include "iostream"
using namespace std;
class A
{
public:
virtual int get(){return 0;}
};
class B:public A
{
public:
int get(){return 1;}
};
void main()
{
B b;
A*p=&b;
cout<<p->get()<<endl;
cout<<p->A::get(); //成员名限定会强制使用静态联编来调用类A的成员函数
}
using namespace std;
class A
{
public:
virtual int get(){return 0;}
};
class B:public A
{
public:
int get(){return 1;}
};
void main()
{
B b;
A*p=&b;
cout<<p->get()<<endl;
cout<<p->A::get(); //成员名限定会强制使用静态联编来调用类A的成员函数
}
本文通过一个C++示例介绍了虚函数如何实现动态联编以及如何使用成员名限定符来强制调用基类的方法实现静态联编。示例中定义了两个类A和B,通过指针调用get方法展示了两种不同的行为。
2万+

被折叠的 条评论
为什么被折叠?



