嵌套类的访问(行为类似于朋友,但不是) [英] Access of nested classes (which behave like friends, but aren't)

查看:89
本文介绍了嵌套类的访问(行为类似于朋友,但不是)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在没有长时间延迟的情况下,我不知道为什么它会执行以下操作:

#include <iostream>

class A {
private:
  void print() { std::cout << "A.print() called" << std::endl; };
public:
  template<typename Foo>
  class B; //NOTE: no friend!
public:
  A();
  B<double>*  bd;
  B<int>*     bi;
}; 

template<typename Foo>
class A::B{
  A* callback;
public:
  B(A* a):callback(a){};
  void print() { callback->print(); }; // Why is this working ???
};

A::A():bd(new B<double>(this)),bi(new B<int>(this)){}

int main(int argc, char **argv)
{
  A a;
//   a.print(); // error: ‘void A::print()’ is private
  a.bd->print();
  a.bi->print();

  A::B<char> c(&a);
  c.print();

  A::B<double> d = *a.bd;
  d.print();

  return 0;
}

好吧,它创建了这个输出:

A.print() called
A.print() called
A.print() called
A.print() called

但是为什么?

背景

最初,我遇到一个与friend有关的问题时,便从兔子洞开始了旅程.因此,我阅读了朋友声明未转发声明(以及提到的答案此处).因此,在尝试建立一个简单的示例(您在上面看到的结果)时,我发现实际上似乎根本不需要friend.

问题

这是底线问题:为什么A::B的实例为什么可以访问A的私有函数A::print()?(尽管我确实意识到我可能会误解什么我的孩子是-孩子,而不是 base 派生)

解决方案

因为嵌套类是封闭类的成员

标准$ 11.7.1

嵌套类是成员,因此具有与任何其他成员相同的访问权限.封闭类的成员对嵌套类的成员没有特殊的访问权限;应遵循通常的访问规则."

和通常的访问规则指定:

类的成员也可以访问该类可以访问的所有名称..."

在标准中给出了

特定示例:

class E {
    int x;
    class B { };

    class I {
        B b; // OK: E::I can access E::B
        int y;
        void f(E* p, int i) {
            p->x = i; // OK: E::I can access E::x
        }
    };
}

Without long delay, here the code which I have no clue why it does what it does:

#include <iostream>

class A {
private:
  void print() { std::cout << "A.print() called" << std::endl; };
public:
  template<typename Foo>
  class B; //NOTE: no friend!
public:
  A();
  B<double>*  bd;
  B<int>*     bi;
}; 

template<typename Foo>
class A::B{
  A* callback;
public:
  B(A* a):callback(a){};
  void print() { callback->print(); }; // Why is this working ???
};

A::A():bd(new B<double>(this)),bi(new B<int>(this)){}

int main(int argc, char **argv)
{
  A a;
//   a.print(); // error: ‘void A::print()’ is private
  a.bd->print();
  a.bi->print();

  A::B<char> c(&a);
  c.print();

  A::B<double> d = *a.bd;
  d.print();

  return 0;
}

Well, it creates this ouput:

A.print() called
A.print() called
A.print() called
A.print() called

But why?

Background

I initially started my journey down the rabbit hole when I encountered a problem which I through to have to do with friends. So I read friend declaration not forward declaring (and the mentioned answers here and here). So while trying to set up an easy example (the result of which you see above), I found that I actually don't seem to need friend at all.

Question

So here is the bottom line question: Why does an instance of A::B have access to A's private function A::print()? (although I do realize that I might misunderstand what my children are--children as opposed to base vs. derived)

解决方案

because nested class is a member of the enclosing class

standard $11.7.1

"A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be obeyed"

and the usual access rules specify that:

"A member of a class can also access all the names to which the class has access..."

specific examples has been given in the standard:

class E {
    int x;
    class B { };

    class I {
        B b; // OK: E::I can access E::B
        int y;
        void f(E* p, int i) {
            p->x = i; // OK: E::I can access E::x
        }
    };
}

这篇关于嵌套类的访问(行为类似于朋友,但不是)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆