<small id='1c77T'></small><noframes id='1c77T'>

<i id='1c77T'><tr id='1c77T'><dt id='1c77T'><q id='1c77T'><span id='1c77T'><b id='1c77T'><form id='1c77T'><ins id='1c77T'></ins><ul id='1c77T'></ul><sub id='1c77T'></sub></form><legend id='1c77T'></legend><bdo id='1c77T'><pre id='1c77T'><center id='1c77T'></center></pre></bdo></b><th id='1c77T'></th></span></q></dt></tr></i><div id='1c77T'><tfoot id='1c77T'></tfoot><dl id='1c77T'><fieldset id='1c77T'></fieldset></dl></div>

  • <tfoot id='1c77T'></tfoot>
    <legend id='1c77T'><style id='1c77T'><dir id='1c77T'><q id='1c77T'></q></dir></style></legend>

        <bdo id='1c77T'></bdo><ul id='1c77T'></ul>

        嵌套类对封闭类私有数据成员的访问

        Nested class#39; access to enclosing class#39; private data members(嵌套类对封闭类私有数据成员的访问)

          <small id='RMhc1'></small><noframes id='RMhc1'>

          <i id='RMhc1'><tr id='RMhc1'><dt id='RMhc1'><q id='RMhc1'><span id='RMhc1'><b id='RMhc1'><form id='RMhc1'><ins id='RMhc1'></ins><ul id='RMhc1'></ul><sub id='RMhc1'></sub></form><legend id='RMhc1'></legend><bdo id='RMhc1'><pre id='RMhc1'><center id='RMhc1'></center></pre></bdo></b><th id='RMhc1'></th></span></q></dt></tr></i><div id='RMhc1'><tfoot id='RMhc1'></tfoot><dl id='RMhc1'><fieldset id='RMhc1'></fieldset></dl></div>
          <legend id='RMhc1'><style id='RMhc1'><dir id='RMhc1'><q id='RMhc1'></q></dir></style></legend>

          <tfoot id='RMhc1'></tfoot>
                <bdo id='RMhc1'></bdo><ul id='RMhc1'></ul>
                    <tbody id='RMhc1'></tbody>
                  本文介绍了嵌套类对封闭类私有数据成员的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在实现一个嵌套类时遇到问题,该类的构造函数是用一些封闭类的私有数据成员初始化的.

                  I'm having trouble implementing a nested class who's constructor is initialized with some of the enclosing class' private data members.

                  示例:

                  Header File:
                  class Enclosing {
                     //...Public members
                     //...Private members
                     int x, int y
                     class Inner; // Declaration for nested class
                  };
                  
                  Impl. File:
                  // Stuff...
                  class Enclosing::Inner {
                      explicit Inner() : foo(x), bar(y) // foo and bar are data members of Inner
                      //...
                  };
                  

                  我收到一个非静态数据成员的无效使用错误.在嵌套类访问其封闭类的成员时,我是否遗漏了什么?

                  I get an invalid use of non-static data member error. Is there something I'm missing when it comes to nested class access to its enclosing class' members?

                  推荐答案

                  Member xyEnclosure 的非静态数据成员,这意味着它们只存在于 Enclosure 类的具体对象中.没有具体的对象,xy 都不存在.同时,您试图在没有对象的情况下引用 xy .这是不可能的,这就是编译器试图告诉你的.

                  Member x and y are non-static data member of Enclosing, which means that they only exist within a concrete object of Enclosing class. Without a concrete object, neither x nor y exist. Meanwhile, you are trying to refer to x and y without an object. That can't be done, which is what the compiler is trying to tell you.

                  如果你想从 xy 初始化成员 Inner::fooInner::bar,您必须将 Enclosure 类型的具体对象传递给 Inner 的构造函数.例如

                  If you want to initialize members Inner::foo and Inner::bar from x and y, you have to pass a concrete object of Enclosing type into the Inners constructor. For example

                  class Enclosing::Inner {    
                    explicit Inner(const Enclosing& e) : foo(e.x), bar(e.y) 
                      {}
                    //...
                  };
                  

                  额外说明:在原来的C++98中,内部类没有访问外部类的特殊权限.使用 C++98 编译器,您要么必须为内部类提供必要的特权(友谊),要么将成员 xy 公开为公共.但是,这种情况被归类为缺陷在 C++98 中,内部类应该拥有对外部类成员(甚至是私有成员)的完全访问权限.因此,您是否需要对访问权限做任何额外的事情取决于您的编译器.

                  Extra note: in the original C++98 the inner class has no special privileges is accessing the outer class. With C++98 compiler you'd either have to give the inner class the necessary privileges (friendship) or expose the members x and y as public. However, this situation was classified as a defect in C++98, and it was decided that inner classes should have full access to outer class members (even private ones). So, whether you have to do anything extra with regard to access privileges depends on your compiler.

                  这篇关于嵌套类对封闭类私有数据成员的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)

                  <legend id='D1Ikh'><style id='D1Ikh'><dir id='D1Ikh'><q id='D1Ikh'></q></dir></style></legend>
                  • <tfoot id='D1Ikh'></tfoot>

                      <small id='D1Ikh'></small><noframes id='D1Ikh'>

                          <bdo id='D1Ikh'></bdo><ul id='D1Ikh'></ul>

                        • <i id='D1Ikh'><tr id='D1Ikh'><dt id='D1Ikh'><q id='D1Ikh'><span id='D1Ikh'><b id='D1Ikh'><form id='D1Ikh'><ins id='D1Ikh'></ins><ul id='D1Ikh'></ul><sub id='D1Ikh'></sub></form><legend id='D1Ikh'></legend><bdo id='D1Ikh'><pre id='D1Ikh'><center id='D1Ikh'></center></pre></bdo></b><th id='D1Ikh'></th></span></q></dt></tr></i><div id='D1Ikh'><tfoot id='D1Ikh'></tfoot><dl id='D1Ikh'><fieldset id='D1Ikh'></fieldset></dl></div>
                            <tbody id='D1Ikh'></tbody>