Accessing a base class member in derived class

2019-03-11 05:28发布

I have a simple class as below

class A {
        protected:
        int x;
        };

class B:public A
        {
        public:
        int y;
      void sety(int d)
        {
        y=d;
        }
        int gety(){ return y;}
        };

int main()  
{
B obj;
obj.sety(10);
cout<<obj.gety();
getch();
}

How can I set the value of the protected instance variable A::x from an instance of the derived class B without creating an instance of class A.

EDIT: Can we access the value of A::x using the object of B? Like obj.x?

4条回答
三岁会撩人
2楼-- · 2019-03-11 05:56

B is an A, so creating an instance of B is creating an instance of A. That being said, I'm not sure what your actual question is, so here's some code that will hopefully clarify things:

class A
{
protected:
    int x;
};

class B : public A
{
public:
    int y;

    int gety() const { return y; }
    void sety(int d) { y = d; }

    int getx() const { return x; }
    void setx(int d) { x = d; }
};

int main()
{
    B obj;

    // compiles cleanly because B::sety/gety are public
    obj.sety(10);
    std::cout << obj.gety() << '\n';

    // compiles cleanly because B::setx/getx are public, even though
    // they touch A::x which is protected
    obj.setx(42);
    std::cout << obj.getx() << '\n';

    // compiles cleanly because B::y is public
    obj.y = 20;
    std::cout << obj.y << '\n';

    // compilation errors because A::x is protected
    obj.x = 84;
    std::cout << obj.x << '\n';
}

obj can access A::x just as an instance of A could, because obj is implicitly an instance of A.

查看更多
劳资没心,怎么记你
3楼-- · 2019-03-11 05:57

You can just refer to it simply as x in class B

For example:

class B : public A
{
public:
    ...

    void setx(int d)
    {
        x=d;
    }
};
查看更多
等我变得足够好
4楼-- · 2019-03-11 06:03

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).

查看更多
来,给爷笑一个
5楼-- · 2019-03-11 06:15

Note that B does not have FULL access to A::x. It can only access that member through an instance of a B, not anything of type A or deriving from A.

There is a workaround you can put in:

class A
{
  protected:
   int x;
   static int& getX( A& a )
   {
      return a.x;
   }

   static int getX( A const& a )
   {
     return a.x;
   }
};

and now using getX, a class derived from A (like B) can get to the x member of ANY A-class.

You also know that friendship is not transitive or inherited. The same "workaround" can be made for these situations by providing access functions.

And in your case you can actually provide "public" access to the x through your B by having public functions that get to it. Of course in real programming it's protected for a reason and you don't want to give everything full access, but you can.

查看更多
登录 后发表回答