Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access base class's protected members through derived class? [duplicate]

Tags:

c++

I'm trying several programs about inheritance, and it turned out that the following caused an error but I don't really know the rationale.

#include <iostream>

using namespace std;

class Base {
protected:
    int x = 0;
};

class Derived: public Base {

    // OK: access protected member via this
    void g() { cout<<x; } 

    // OK: access protected member of other Derived
    void h(Derived& d) { cout<<d.x; } 

    // FAIL: access Base class's protected member, why?
    void f(Base& b) { cout<<b.x; } 
};

int main() {}

I expect that the Derived class could access the ​Base class's public or protected data members and member function.

However it didn't work as what I was thinking about, could anyone help me light up my concepts?

like image 497
Sean Tsai Avatar asked Apr 05 '19 10:04

Sean Tsai


People also ask

Can base class access protected members of derived class?

Protected members in a class are similar to private members as they cannot be accessed from outside the class. But they can be accessed by derived classes or child classes while private members cannot.

Can protected data members be accessed from derived class?

Protected members that are also declared as static are accessible to any friend or member function of a derived class. Protected members that are not declared as static are accessible to friends and member functions in a derived class only through a pointer to, reference to, or object of the derived class.

How do you access base class members in a derived class?

You can use both a structure and a class as base classes in the base list of a derived class declaration: If the derived class is declared with the keyword class , the default access specifier in its base list specifiers is private .

How are protected members of a base class access it in the derived class when inherited privately in C++?

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 .


3 Answers

There is not more to it than you already discovered. Derived instances may acces their protected members and those of other derived instances but not those of base class instances. Why? Because thats how protected works by definition.

For more details I refer you to cppreference (emphasize mine):

A protected member of a class Base can only be accessed

1) by the members and friends of Base

2) by the members and friends (until C++17) of any class derived from Base, but only when operating on an object of a type that is derived from Base (including this)

like image 66
463035818_is_not_a_number Avatar answered Oct 23 '22 11:10

463035818_is_not_a_number


void f(Base& b) {cout<<b.x;}

Here you are trying to access a protected member of a different class. It does not matter that you also share the same base class. (still looking for a source)

void g() {cout<<x;}

In this example you are acccessing your own private member. (protected members of base class are inherited and protected in derived class)

void h(Derived& d) {cout<<d.x;}

Here you are accessing the private member of the same class. But for more on this look at this post: Access private elements of object of same class

like image 31
FloIsAwsm Avatar answered Oct 23 '22 11:10

FloIsAwsm


From this documentation

A protected member of a class Base can only be accessed

  1. by the members and friends of Base

    this is not your case

  2. by the members and friends (until C++17) of any class derived from Base, but only when operating on an object of a type that is derived from Base (including this)

    this is your case, but the argument b is not such a derived type

The reason for protected member access is to allow a base class to define an interface for use by derived classes. That's not the same as allowing every different derived type special access to every base class object.

like image 37
Useless Avatar answered Oct 23 '22 10:10

Useless