博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
protected: C++ access control works on per-class basis, not on per-object basis
阅读量:6965 次
发布时间:2019-06-27

本文共 2283 字,大约阅读时间需要 7 分钟。

一个很简单的问题:

//为什么BASE::foo()中可以直接通过p访问val? 看本记录标题,这个问题困扰了很长一段时间,终于解决 class BASE{
      private:            int val; public:void foo(BASE *p) { int w = p->val; } };

 

同学参加一场笔试,抛出个问题,丫凡和我讨论了下,丫凡在stackoverflow上找到了答案……

如下内容引述自:http://stackoverflow.com/questions/6986798/subtle-c-inheritance-error-with-protected-fields                        

==========================================================================

Below is a subtle example of accessing an instance's protected field x. B is a subclass of A so any variable of type B is also of type A. Why can B::foo() access b's x field, but not a's x field?

class A {    protected:          int x;};class B : public A {    protected:      A *a;      B *b;    public:      void foo()       {          int u = x;     // OK : accessing inherited protected field x          int v = b->x;  // OK : accessing b's protected field x          int w = a->x;  // ERROR : accessing a's protected field x      }};

Why can the B::foo() access the members of the contained class B pointer b?

The rule is:
In C++ access control works on per-class basis, not on per-object basis.
So an instance of class B will always have access to all the members of another instance of class B.

 

 

class A {    protected:        int x;};class B : public A {    public:        void foo()         {            int u = x;     // OK : accessing inherited protected field        }};

Since child is inheriting parent, child gets x. Hence you can access x directly in foo() method of child. This is the concept of protected variables. You can access protected variables of parent in child directly. Note : Here I am saying you can access x directly but not through A's object! Whats the difference ? Since, x is protected, you cannot access A's protected objects outside A. Doesnt matter where it is - If its main or Child . That's why you are not able to access in the following way.

Here comes an interesting concept. You can access a private variable of a class using its object with in the class!

class dummy {    private :       int x;    public:      void foo()       {          dummy *d;          int y = d->x; // Even though x is private, still you can access x from object of d - But only with in this class. You cannot do the same outside the class.       }};

 

转载地址:http://mvgil.baihongyu.com/

你可能感兴趣的文章
docker在centos7上安装私服镜像各种操蛋修复
查看>>
关于Logstash中grok插件的正则表达式例子
查看>>
数据挖掘中的KNN
查看>>
堪比锦衣卫的服务追踪【我身边的戴尔企业级解决方案】
查看>>
漫谈vfp程序界面及设计观
查看>>
性能魔方mmTrix:应用性能与用户体验“石器时代”终结者
查看>>
1.大道至简的数据处理工具(Power Query)
查看>>
python第二阶段第五天额外增加
查看>>
开始写博客啦
查看>>
Cisco_IP_Communicator_软电话配置过程全解
查看>>
友元函数的产生和理解
查看>>
系统运维问题诊断集合
查看>>
springboot 2.0+elasticseach 5.5.0集群环境搭建示例(附源码)
查看>>
利用Hadoop提供的RPC API实现简单的RPC程序
查看>>
avg count null值计算
查看>>
SfB迁移CMS中央存储数据库镜像配置
查看>>
Gartner:2013-2014年全球MSS市场分析
查看>>
std::function源码分析
查看>>
Android的Activity组件启动,切换和值传递学习
查看>>
SQL Server中truncate、delete和drop的异同点
查看>>