C++新的和删除的问题3

预测产量?

null

#include <iostream>
using namespace std;
class Test
{
int x;
Test() { x = 5;}
};
int main()
{
Test *t = new Test;
cout << t->x;
}


(A) 编译错误 (B) 5. (C) 垃圾值 (D) 0 答复: (A) 说明: 存在编译器错误:Test::Test()是私有的。

new调用构造函数。在类测试中,构造函数是私有的(注意,默认访问在C++中是私有的)。

这个问题的小测验

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享