c++ - 内部/嵌套类-私有(private)和公共(public)之间的区别

标签 c++

我有内部嵌套类,它是私有(private)的:

   class Client{
   private:
      class Inner;
      Inner *i;

如果我将 Inner 类公开而 Inner *i 保持私有(private),究竟会发生什么?会发生什么,对程序执行有什么影响?

最佳答案

考虑以下案例用法:

class Client
{
private:
    class Inner {};
    Inner *i;

    class Inner2 {};
public:
    class Inner3 {};
    Inner3 *j;

public:
    Inner2 *k;
};

void main()
{
    Client c;

    c.i = nullptr; //error: you cant access the private members

    c.j = nullptr; //ok: member is public
    c.k = nullptr; //ok: same here, member is public, even if it's type is private

    Client::Inner3 i3;//ok: to declare since Inner3 is declared public

    Client::Inner2 i3;//error: can't access private members of type declarations

}

除此之外,程序的执行在任何情况下都不受私有(private)/公共(public)或 protected 影响。

关于c++ - 内部/嵌套类-私有(private)和公共(public)之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19978696/

相关文章:

c++ - #if 预处理器指令可以嵌套在 C++ 中吗?

c++ - 将读取的 char 转换为 int c++

c++ - 编译时映射和逆映射值

c++ - 在 SQL Server 表中进行更改时通知 C++ 应用程序

c++ - 重载函数歧义

c++ - 即使注册成功,CreateWindowEx 也会失败并出现错误 1407?

c++ - 使用 initializer_list 初始化数组?

c++ - 如何更新已在 C++ 中使用 TrackPopupMenu 显示的上下文菜单

c++ - 继承/多态——子类的调用方法

c++ - 如何使用 C++ 逐行并行处理