accessibility about conversion from derived class to base class

Hello everyone, I am trying to understand the accessibility of derived class to base class. But it seems confusing.
If class Temp publicly inherited from Base, for user code, this conversion can happen.
If the inheritance is private, for user code it can not happen.
For the friend function in Derived:
friend void print(const Temp &t){Base B = t; ...},
here Base b = t; is kind of user code for class Temp, I think. So it is consistent with the statements above.

In this way, when the inheritance of Temp from Base is protected, this conversion should not happen and the compiler should fail, but the fact is that the print function worked. What is the logic here ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

class Base
{
public:
    int pub = 3;
};
class Temp : protected Base     // here for friend void print(const Temp &t)
{                               // in Derived class,
public:                         // isn't it similar to private Base ? ? ?
    int pub_t = 30;             // while for Temp:private Base, we can not do 
};                              // the conversion Base b = t;
class Derived : private Temp
{
public:
    friend void print(const Temp &t);
};
void print(const Temp &t) { Base b = t; cout << b.pub << endl; }

int main()
{
    Temp t1;
    print(t1);
    return 0;
}
Registered users can post here. Sign in or register to post.