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?
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.
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.
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 .
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 .
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)
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
From this documentation
A protected member of a class Base can only be accessed
by the members and friends of Base
this is not your case
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With