请考虑此代码。
#include <iostream>
using namespace std;
class sample
{
public:
sample()
{
cout << "consructor called" << endl;
}
void test()
{
cout << "Test function" << endl;
}
};
int main()
{
sample *s = nullptr;
try
{
s = new sample[50000000000000000000];
cout << "allocated" << endl;
}
catch(bad_alloc& ba)
{
cout << ba.what() << endl;
}
s[1].test();
return 0;
}
抛出bad_alloc异常,但是s [1] .test();在屏幕上打印测试功能。我怀疑的是,如果分配不成功,那么对象如何调用成员函数。