64,683
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <ctime>
using namespace std;
class B
{
public:
B():x(0){}
B(int value):x(value){}
void set(int value){
x = value;
}
protected: // or private
int x;
};
int main()
{
B b1;
B b2(10);
b1.set(b2.x);
return 0;
}
b1,b2虽然为同类对象,但他们工作在类的scope的外部,所以b1无法通过调用set成员函数来访问b2中的x。
2. 在类的内部scope, 即像你的程序那样,在类的定义的内部,是可以访问的。