#include <iostream>
using namespace std;
class B
{
B();
public:
virtual void print()=0;
};
void B::print()
{
cout << "B::print";
}
int main()
{
B *bp;
bp->B::print(); /* Type-A works fine */
bp->print(); /* Type-B segmentation fault */
return 0;
}
在上面的代码中,我试图通过“bp”调用纯虚函数。现在在 main 函数中有两种类型的调用(Type-A,Type-B)。我的问题是为什么 A 有效而 B 无效。此外,为什么编译器允许在不创建对象的情况下调用非静态函数。