Accessing a base class member in derived class

前端 未结 4 1481
[愿得一人]
[愿得一人] 2020-12-28 17:20

I have a simple class as below

class A {
        protected:
        int x;
        };

class B:public A
        {
        public:
        int y;
      void s         


        
4条回答
  •  醉话见心
    2020-12-28 17:45

    A::x is protected, so not accessible from outside, neither as A().x or B().x. It is however accessible in methods of A and those directly inheriting it (because protected, not private), e.g. B. So, regardless of semantics B::sety() may access it (as plain x or as A::x in case of shadowing by a B::x or for pure verbosity).

提交回复
热议问题