相关疑难解决方法(0)

C++中的内部类是自动的朋友吗?

如果我在C++中定义一个内部类,它是否自动成为包含它的类的朋友?例如,这是合法的:

class Outer {
public:
    class Inner {
    public:
        void mutateOuter(Outer& o);
    };

private:
    int value;
};

void Outer::Inner::mutateOuter(Outer& o) {
    o.value ++; // Legal?  Or not?
}
Run Code Online (Sandbox Code Playgroud)

我问,因为在我试过的一些编译器(VS2003)上,这段代码不起作用,但我至少听说过它在某些编译器上有效.我无法在C++规范中找到关于此的相关部分,如果有人能够引用某些特定内容,那就是说它是合法的还是不合法的.

c++ friend nested-class

68
推荐指数
3
解决办法
2万
查看次数

朋友访问受保护的嵌套类

我有以下C++代码:

class A {
 protected:
  struct Nested {
    int x;
  };
};

class B: public A {
  friend class C;
};

class C {
  void m1() {
    B::Nested n; // or A::Nested
  }
};
Run Code Online (Sandbox Code Playgroud)

使用g ++ 4.4编译这个片段,无论我在m1中使用B :: Nested还是A :: Nested,都没有区别.Clang接受B::Nested,但如果我不编译A::Nested.这是g ++或clang中的错误吗?

c++ friend

11
推荐指数
1
解决办法
3173
查看次数

标签 统计

c++ ×2

friend ×2

nested-class ×1