Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing protected members in a derived class

Tags:

c++

I ran into an error yesterday and, while it's easy to get around, I wanted to make sure that I'm understanding C++ right.

I have a base class with a protected member:

class Base {   protected:     int b;   public:     void DoSomething(const Base& that)     {       b+=that.b;     } }; 

This compiles and works just fine. Now I extend Base but still want to use b:

class Derived : public Base {   protected:     int d;   public:     void DoSomething(const Base& that)     {       b+=that.b;       d=0;     } }; 

Note that in this case DoSomething is still taking a reference to a Base, not Derived. I would expect that I can still have access to that.b inside of Derived, but I get a cannot access protected member error (MSVC 8.0 - haven't tried gcc yet).

Obviously, adding a public getter on b solved the problem, but I was wondering why I couldn't have access directly to b. I though that when you use public inheritance the protected variables are still visible to the derived class.

like image 736
miked Avatar asked Jul 14 '10 15:07

miked


People also ask

How are protected members of class accessed in the derived class?

If a class is derived privately from a base class, all protected base class members become private members of the derived class. Class A contains one protected data member, an integer i . Because B derives from A , the members of B have access to the protected member of A .

Can we access protected member derived class in Java?

We can access protected members of a class in another class that is present in the same package.

Do derived classes inherit protected members?

public inheritance makes public members of the base class public in the derived class, and the protected members of the base class remain protected in the derived class. protected inheritance makes the public and protected members of the base class protected in the derived class.

How do I access protected classes?

Accessing a protected class. Accessing display function from the same package but different. Accessing display function from a different package. Accessing a protected class by overriding to sub-class within the same package.


2 Answers

A class can only access protected members of instances of this class or a derived class. It cannot access protected members of instances of a parent class or cousin class.

In your case, the Derived class can only access the b protected member of Derived instances, not that of Base instances.

Changing the constructor to take a Derived instance will solve the problem.

like image 193
SLaks Avatar answered Sep 19 '22 14:09

SLaks


protected members can be accessed:

  • through this pointer
  • or to the same type protected members even if declared in base
  • or from friend classes, functions

To solve your case you can use one of last two options.

Accept Derived in Derived::DoSomething or declare Derived friend to Base:

class Derived;  class Base {   friend class Derived;   protected:     int b;   public:     void DoSomething(const Base& that)     {       b+=that.b;     } };  class Derived : public Base {   protected:     int d;   public:     void DoSomething(const Base& that)     {       b+=that.b;       d=0;     } }; 

You may also consider public getters in some cases.

like image 28
Sergei Krivonos Avatar answered Sep 17 '22 14:09

Sergei Krivonos