English 中文(简体)
获取外部班内班级的私人成员数据
原标题:Access to private member data of outer class in inner class

该法典有:

#include <iostream>

class Outer{
    int a; // private data member of class Outer
public:
    Outer(): a(55){}
    class Inner{
    public:
        void fun(Outer ob){
            std::cout << ob.a << std::endl;
        }
    };
};

int main() {

    Outer::Inner object;
    object.fun(Outer()); // prints 55
    //std::cout << (Outer().a) << std::endl; error:  int Outer::a  is private

    return 0;
} 

Why Inner class has access to private member data a of class Outer? Following this article XL C/C++ V8.0 for Linux, it should not compile, however it compiles on g++ 4.4.0.

最佳回答

根据该文件第XL C/C++号。 V8.0不支持C++11,见“Language标准合规”一节。

汇编者支持C和C++的以下方案拟订语言规格:

  • ISO/IEC 9899:1999 (C99)
  • ISO/IEC 9899:1990 (referred to as C89)
  • ISO/IEC 14882:2003 (referred to as Standard C++)
  • ISO/IEC 14882:1998, the first official specification of the language (referred to as C++98)

现行标准(ISO/IEC 14882:2011 11.7):

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 (Clause 11) shall be obeyed.

在以前的语文标准中,是否允许这种接触,或者至少不清楚是否应当允许这种接触,这取决于你们的解释。

问题回答

C++03 Standard 1.1.8/1: [}.access.nest]

www.un.org/Depts/DGACM/index_spanish.htm 免职人员不能特别接触穿戴“ 封闭式班子的成员不能特别接触某类成员;通常的准入规则(clause 11)应服从。

但这是一个缺陷:

http://www. open-std.org/jtc1/sc22/wg21/docs/cwg_defects.htmlp45”rel=“nofollow”>45. 无障碍上门 >>>

在C++11中,已经纠正了这一情况:在C++11中,加固的班级确实能够接触到封闭式班级的私人成员(尽管封闭式班级仍然无法接触到被nes门班的私人成员)。

C++11 标准11.7 Nest类:

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 (Clause 11) shall be obeyed. [

class E {
  int x;
  class B { };
  class I {
    B b; // OK:E::I can accessE::B
    int y;
    void f(E* p, int i) {
      p->x = i; // OK:E::I can accessE::x
    }
  };
  int g(I* p) {
    return p->y; // error:I::y is private
  }
};
—end example]

The example is similar to the one you have in question and it clearly shows its valid behavior.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签