大家来拿分了噢,超简单
#include <iostream.h>
class X
{
public:
void rotate(){cout < < "The base class 's rotate was called. " < <endl;};
};
class Z : public X
{
public:
void rotate(){cout < < "The derived class 's rotate was called. " < <endl;};
};
void rotate(X datum, X *pointer, X &reference)
{
cout < < "The pointer: " < <endl;
(*pointer).rotate();
cout < < "The reference: " < <endl;
reference.rotate();
cout < < "The object: " < <endl;
datum.rotate();
}
main()
{
Z z;
rotate(z, &z, z);
return 0;
}
输出结果:
The pointer:
The base class 's rotate was called.
The reference:
The base class 's rotate was called.
The object:
The base class 's rotate was called.
为什么指针和引用没有调用派生类的rotate?
[解决办法]
因为不是虚函数,静态邦定。所以 是调用基类 函数
[解决办法]
virtual void rotate(){cout < < "The base class 's rotate was called. " < <endl;
这样就能调用了!