10
class Base {
public:
class FirstBase {
friend class Base;
int x = 10;
};
class SecondBase : public FirstBase {
public:
SecondBase() : FirstBase() {}
void t() { std::cout << FirstBase::x; }
};
};
This code compiles and works, but I don’t understand why it works. Can explain or cite sources to read?
I use gcc version 11.4.0 std=C++17
2
2 Answers
Reset to default
14
Base
is a friend of FirstBase
, and SecondBase
can access anything Base
can access.
From the latest draft:
class.access, item 2:
A member of a class can also access all the members to which the class has access.
(Note that "nested" classes are members of the enclosing class.)
with the footnote
Access permissions are thus transitive and cumulative to nested and local classes.
Note that the inheritance is irrelevant – this is also fine:
class Base {
public:
class FirstBase {
friend class Base;
int x = 10;
};
class SecondBase {
public:
SecondBase() {}
void t(FirstBase fb) { std::cout << fb.x; }
};
};
1
-
friendship is not transitive, aside from that, true enough.
– Deduplicator1 hour ago
9
Because Base
is a friend of FirstBase
. So inside Base
you can access everything in FirstBase
. And SecondBase
is inside Base
so SecondBase
can access FirstBase
. If you remove the friend relationship or you move SecondBase
outside of Base
, it’ll break immediately as you expect.
class Base {
public:
class FirstBase {
friend class Base; // This vill give access to FirstBase private members inside Base.
int x = 10;
};
// SecondBase is inside Base so you can access FirstBase private members due to the friend relationship
class SecondBase : public FirstBase {
public:
SecondBase() : FirstBase() {}
void t() { std::cout << FirstBase::x; }
};
};
Not the answer you're looking for? Browse other questions tagged
or ask your own question.
or ask your own question.
related stackoverflow.com/q/486099/4117728
12 hours ago
note that the inheritance isn't essential for the example. You can make
x
a static member andSecondBase
will still have access godbolt.org/z/zWxsYWjo511 hours ago
|