#include "stdafx.h"
#include <iostream>
using
namespace
std;
class
A
{
public
:
virtual
void
test()
{
cout<<
"A:test()"
<<endl;
};
void
foo()
{
cout<<
"A:foo()"
<<endl;
};
};
class
B :
public
A
{
public
:
void
test(){
cout<<
"B:test()"
<<endl;
};
void
foo(){
cout<<
"B:foo()"
<<endl;
};
};
int
_tmain(
int
argc, _TCHAR* argv[])
{
A* a =
new
B;
a->test();
//虚函数,动态绑定,调用动态类型(实际对象类型)B版本
a->foo();
//非虚函数,调用静态类型(指针类型)A的版本
B* b =
new
B;
b->test();
//虚函数,动态绑定,调用动态类型B版本
b->foo();
//非虚函数,调用静态类型(指针类型)A的版本
b->A::foo();
//这两个指定是调用A类版本
b->A::test();
return
0;
}
刚才遇到一个关于多态虚函数的例子,发现学的东西忘了好多~~在这里引用一下增强记忆。
原文:http://www.cnblogs.com/lijuntobenumber/p/5225719.html