1

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

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

如果我将类 Inner 设为 public 而 Inner *i 将保持私有,究竟会发生什么?会发生什么,在程序执行方面会产生什么影响?

4

2 回答 2

1

如果您Client::Inner公开,它的名称将变得可访问。它对i;的可访问性没有影响

class Client{
public:
    class Public;
private:
    class Private;
    Public a;
    Private b;
};

int main()
{
    Client::Public a;
    Client::Private b; // error

    Client c;
    c.a;  // error
    c.b;  // error
}
于 2013-11-14T13:23:57.963 回答
1

考虑以下案例用法:

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

}

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

于 2013-11-14T13:24:26.380 回答