c++关于protected成员的访问问题

cljklqwer1 2015-09-11 04:06:37
#include <iostream>
using namespace std;

class B{
protected:
int test = 0;
public:
void get(int& i) {
i = test;
}
void set(int i) {
test = i;
}
void fun(B& b) {
test = b.test;//这里为什么可以直接访问?
};

};

int main() {
B b1;
b1.set(10);
B b2;
b2.fun(b1);
int temp=0;
b2.get(temp);
cout << temp<<endl;
system("pause");
return 0;
}
编译后b2的test 的值是10,但在fun()中是直接访问b1的test成员 为什么没有出错?
...全文
292 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
heroesjun 2015-09-11
  • 打赏
  • 举报
回复
引用 6 楼 cljklqwer1 的回复:
[quote=引用 2 楼 heroesjun 的回复:] 因为fun()是B类的成员函数,成员函数可以访问该类的所有成员变量。
但是b2调用fun访问了b1的私有成员.. 这可以吗..[/quote] 这要分两种情况: 1. 在类的外部的scope访问同类对象的protected或private成员变量,这个是不允许的,比如:
#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, 即像你的程序那样,在类的定义的内部,是可以访问的。
cljklqwer1 2015-09-11
  • 打赏
  • 举报
回复
受教了
_lee_chong 2015-09-11
  • 打赏
  • 举报
回复
引用 5 楼 cljklqwer1 的回复:
[quote=引用 3 楼 lc316546079 的回复:] [quote=引用 1 楼 cljklqwer1 的回复:] 求真相....
你为什么认为不能,test 是他自己的成员能访问有什么问题么?[/quote] 但是b2调用fun访问了b1的私有成员.. 这可以吗..[/quote] 可以的,这里的访问权限指的是写代码的地方,这里调用test的代码不是还在b类里么; 这是语法问题,没必要深究;
ztenv 版主 2015-09-11
  • 打赏
  • 举报
回复
引用 6 楼 cljklqwer1 的回复:
[quote=引用 2 楼 heroesjun 的回复:] 因为fun()是B类的成员函数,成员函数可以访问该类的所有成员变量。
但是b2调用fun访问了b1的私有成员.. 这可以吗..[/quote] b2、b1只是实例不同类代码还是同一套。 C++可能没做过多的约束(没有深究这个问题);C#3.x以后就不能这么用了,但不么这么用很带来很多不方便…………。
cljklqwer1 2015-09-11
  • 打赏
  • 举报
回复
引用 2 楼 heroesjun 的回复:
因为fun()是B类的成员函数,成员函数可以访问该类的所有成员变量。
但是b2调用fun访问了b1的私有成员.. 这可以吗..
cljklqwer1 2015-09-11
  • 打赏
  • 举报
回复
引用 3 楼 lc316546079 的回复:
[quote=引用 1 楼 cljklqwer1 的回复:] 求真相....
你为什么认为不能,test 是他自己的成员能访问有什么问题么?[/quote] 但是b2调用fun访问了b1的私有成员.. 这可以吗..
赵4老师 2015-09-11
  • 打赏
  • 举报
回复
protected C++ Specific —> protected: [member-list] protected base-class When preceding a list of class members, the protected keyword specifies that those members are accessible only from member functions and friends of the class and its derived classes. This applies to all members declared up to the next access specifier or the end of the class. When preceding the name of a base class, the protected keyword specifies that the public and protected members of the base class are protected members of the derived class. Default access of members in a class is private. Default access of members in a structure or union is public. Default access of a base class is private for classes and public for structures. Unions cannot have base classes. For related information, see public, private, friend, and Table of Member Access Privileges. END C++ Specific Example // Example of the protected keyword class BaseClass { protected: int protectFunc(); }; class DerivedClass : public BaseClass { public: int useProtect() { protectFunc(); } // protectFunc accessible // from derived class }; void main() { BaseClass aBase; DerivedClass aDerived; aBase.protectFunc(); // Error: protectFunc not // accessible aDerived.protectFunc(); // Error: protectFunc not // accessible in derived class }
_lee_chong 2015-09-11
  • 打赏
  • 举报
回复
引用 1 楼 cljklqwer1 的回复:
求真相....
你为什么认为不能,test 是他自己的成员能访问有什么问题么?
heroesjun 2015-09-11
  • 打赏
  • 举报
回复
因为fun()是B类的成员函数,成员函数可以访问该类的所有成员变量。
cljklqwer1 2015-09-11
  • 打赏
  • 举报
回复
求真相....

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧