c++在线等

来源:百度知道 编辑:UC知道 时间:2024/06/22 02:33:57
9. 指出下面程序段中的错误,并说明出错原因。
class Test{
private: int num;
protected: int p;
};
void fun()
{Test m;
int x=m.p;}

Test m;
必须把Test对象实例化后才能在后面使用。
改为这样:
Test m = new Test;

还有
int x=m.p;
p是protected,外部不能访问。

类的对象 m 没实例化(即 num 和 p 都是未知数),
另外,fun() 不能直接访问 Test 的protected 成员

主要的问题是p是protected,外部不能访问。
而楼上的说Test m = new Test; 这个是JAVA实例的初始化,在C++里不需要这样,Test m 就够了。

你的QQ多少?我加你

一楼你搞什么?
new test返回的是指针地址。应该赋给指针才对;test *p=new test;
二楼说得对;