c++ - 了解 N3690 草案中的 §11.2-4

标签 c++

在 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/

相关文章:

c# - 将 C++ Builder 代码转换为 C# .NET(TComponent、TOjbect、TList 等)

c++ - 没有锁或互斥锁的原子比较

c++ - 不允许向 vector 中添加更多元素

c++ - 使用 libcurl 下载功能,但它不完整

C++ 进程和管道

c++ - 如何使用 std::shared_ptr 实现缓存管理器?

c++ - 找到堆栈的最大高度,使其对于所有三个堆栈都相等

c++ - 为什么这个随机数生成器生成相同的数字?

c++ - 从 C++ 调用 Win32 DLL

C++ map<char, static method pointer>?