(环境:C++ VS2010)特殊投资函数 00000000
#include <iostream>
#include <vector>
using namespace std;
class Security {
public:
virtual ~Security() {}
};
class Stock : public Security {};
class Investment : public Security {
public:
void special() {
cout << "special Investment function" << endl;
}
};
int main() {
Security* p = new Stock;
dynamic_cast<Investment*>(p)->special();
cout << dynamic_cast<Investment*>(p) << endl;
return 0;
}
怎么可能呢?解引用一个空指针,却能得到“正确”的输出而不是崩溃?这是VS2010的特殊“特性”吗?
现在我明白了。我进行了一次测试,发现在“特殊”函数中解引用“this”会导致程序崩溃。
感谢您的帮助。