在 C++ N3690 草案中。
A base class B of N is accessible at R, if
- an invented public member of B would be a public member of N, or
- R occurs in a member or friend of class N, and an invented public member of B would be a private or protected member of N, or
- R occurs in a member or friend of a class P derived from N, and an invented public member of B would be a private or protected member of P, or
- there exists a class S such that B is a base class of S accessible at R and S is a base class of N accessible at R.
有人可以为我提供上述语句的代码吗?
我试过这样做。
class B {
public:
int m;
};
class S: private B {
N r;
};
class N: private S {
void f() {
B* p = this;
}
};
int main()
{
return 0;
}
最佳答案
您的N
不是S
类的成员或好友。你的 r
是,但它不是 N
,它只有 N
类型。
使其成为成员:
struct B {
int m;
};
struct S : private B {
struct N;
};
struct S::N : private S {
void f() {
B* p = this;
}
};
让它成为 friend :
struct B {
int m;
};
struct S : private B {
friend struct N;
};
struct N : private S {
void f() {
B* p = this;
}
};
关于c++ - 了解 N3690 草案中的 §11.2-4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41414072/