如果一个类里面包含了一个对象成员。这个类可以访问这个对象成员的私有数据吗?
1个回答
展开全部
是不可以的,这个写代码试一下最保险。
#include <stdio.h>
class stu1
{
private:
int a;
};
class stu2
{
int b;
class stu1 stu;
public:
void printdata()
{
b=10;
stu.a = 1;
printf("%d\n",b);
printf("%d\n",stu.a);
}
};
int main()
{
class stu2 stu3;
stu3.printdata();
return 0;
}
编译的时候报错
1>------ Rebuild All started: Project: TEST_C, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'TEST_C', configuration 'Debug|Win32'
1>Compiling...
1>main.cpp
1>e:\visual studio 2005\projects\test_c\test_c\main.cpp(17) : error C2248: 'stu1::a' : cannot access private member declared in class 'stu1'
1> e:\visual studio 2005\projects\test_c\test_c\main.cpp(6) : see declaration of 'stu1::a'
1> e:\visual studio 2005\projects\test_c\test_c\main.cpp(4) : see declaration of 'stu1'
1>e:\visual studio 2005\projects\test_c\test_c\main.cpp(20) : error C2248: 'stu1::a' : cannot access private member declared in class 'stu1'
1> e:\visual studio 2005\projects\test_c\test_c\main.cpp(6) : see declaration of 'stu1::a'
1> e:\visual studio 2005\projects\test_c\test_c\main.cpp(4) : see declaration of 'stu1'
1>Build log was saved at "file://e:\Visual Studio 2005\Projects\TEST_C\TEST_C\Debug\BuildLog.htm"
1>TEST_C - 2 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询