请帮助我理解以下问题。
看下面的代码示例:
#include <iostream>
class Shape {
public:
virtual wchar_t *GetName() { return L"Shape"; }
};
class Circle: public Shape {
public:
wchar_t *GetName() { return L"Circle"; }
double GetRadius() { return 100.; }
};
int wmain() {
using namespace std;
auto_ptr<Shape> aS;
auto_ptr<Circle> aC(new Circle);
aS = aC;
wcout << aS->GetName() << L'\t' << static_cast<auto_ptr<Circle>>(aS)->GetRadius() << endl;
return 0;
}
为什么我不允许这样做:
static_cast<auto_ptr<Circle>>(aS)->GetRadius()
编译器(MSVCPP 11):
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory(911): error C2440: 'initializing' : cannot convert from 'Shape *' to 'Circle *'
1> Cast from base to derived requires dynamic_cast or static_cast