Protected inheritance

How does protected inheritance work. Here child_protected inherits Base as protected. Now the public member of Base is protected for child_protected class.
So when I create an object of child_inheritance should public_member be accessible by . operator??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 class Base {

//Public members accessible by everyone
public:
	int public_member;

//Protected members accessible only inside class by class in which it is defined and its child
protected:
	int protected_member;

//Private members are accessible only inside class in which it is defined and by friend members
private:
	int private_member;
};

//Everything child_protected inherits from base is now protected  i.e. public member of Base is protected here
class child_protected : protected Base {
public:
	void print() {
		//Protected members are accessible inside class
		cout << this->protected_member<<endl;
		cout << this->public_member<<endl;
	}
};
When you use protected inheritance:
all public members from Base class are protected in child_protected
all protected members are still protected
all privete members are still private
So, I cant do this
1
2
child_protected cp;
cp.public_member; //This should cause compilation error right? 
http://www.gotw.ca/publications/mill06.htm
https://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance

The protected Base does not really affect the child_protected. It does affect users of child_protected objects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class A 
{
public:
    int x;
};

class B : protected A
{
};

int main() {
  B foo;
  foo.x = 42; // error, B::x is not public
  return 0;
}
Topic archived. No new replies allowed.