采用以下结构和类:
struct TestStruct
{
};
class TestClass
{
public:
TestStruct* testStruct;
};
在 中执行以下操作main
:
TestClass testClass;
if (testClass.testStruct == NULL)
cout << "It is NULL." << endl;
else
cout << "It is NOT NULL.";
输出将是:It is NOT NULL.
。
但是,如果我改为这样做:
TestClass testClass;
if (testClass.testStruct == NULL)
cout << "It is NULL." << endl;
else
cout << "It is NOT NULL." << endl << testClass.testStruct;
输出将是:It is NULL.
。
有趣的是,如果我这样做(与上述基本相同):
TestClass testClass;
if (testClass.testStruct == NULL)
{
cout << "It is NULL." << endl;
}
else
{
cout << "It is NOT NULL." << endl;
cout << testClass.testStruct;
}
输出将是:
It is NOT NULL.
0x7fffee043580.
到底是怎么回事?