简体   繁体   English

从嵌套的 class 访问私有变量到包含 class 的私有方法

[英]Accessing a private variable from nested class to a private method in enclosing class

//In file A.hpp
    class A
    {
        public:
        void random();
        A() {};
        virtual ~A() {};
        class B
        {
            public:
            B(int a, int b) 
              : c(a), d(b) {};
            virtual ~B() {};
            void SetVariable( int alpha )
            {
                beta = alpha;
            }
            private:
            int beta;
            int c; int d;
        };
        
        private:
        void GetVariable( int gamma ); 
    };
    
    int main()
    {
        
    }

I want to access beta in "GetVariable" method.我想以“GetVariable”方法访问测试版。 beta is a private variable in nested class. beta 是嵌套 class 中的私有变量。 I want to access the updated value of beta in GetVariable method.我想在 GetVariable 方法中访问 beta 的更新值。

You cannot.你不能。 Even if the member was public you cannot access a member of one class in the member of a second class unless you have an instance of that second class available.即使该成员是public的,您也无法在第二个 class 的成员中访问一个 class 的成员,除非您有第二个 class 的实例可用。

Your code looks a little complicated because it has inheritance and what not.您的代码看起来有点复杂,因为它有 inheritance 等等。 Let me try to explain by means of a simpler example:让我尝试通过一个更简单的例子来解释:

struct F { 
    struct G {
       int x; 
    };
   
    int get();
};

int main() {
    F::G g{42};
}

Now, similar question: How to return G::x from F::get() ?现在,类似的问题:如何从F::get()返回G::x

You can't.你不能。

What you can do is...你能做的是...

Add a G member to F and return the member of that instance:G成员添加到F并返回该实例的成员:

struct F { 
    struct G {
       int x; 
    };
    G g;  
    int get() {
         return g.x;
    }
};

or pass a G to get :或通过G get

struct F { 
    struct G {
       int x; 
    };
    int get(const G& g) {
         return g.x;
    }
};

Just because G is defined inside F does not make any instance of G a member of a F .仅仅因为G是在F内部定义的,并不会使G的任何实例成为F的成员。


Actually also the nesting is not that relevant because with regards to accessing the member G::x it is similar to, now with private :实际上嵌套也不是那么相关,因为关于访问成员G::x它类似于,现在使用private

 class G {
     int x;
 };

 struct F {
     int get() { /* how to access G::x here?!?! */ }
 };

Now again two issues: First you need an instance of G to access one of its members.现在又是两个问题:首先,您需要一个G的实例来访问其成员之一。 Second, you cannot access it when it is private.其次,当它是私有的时,您无法访问它。 As above, we pass an instance to get and in addition add an accessor:如上所述,我们传递一个实例来get并另外添加一个访问器:

 class G {
     int x;
 public:
     G() : x(42) {}
     int get() const { return x; }
 };

 struct F {
     int get(const G& g) { return g; }
 };

 int main() {
     G g;
     F f;
     int y = f.get(g);
 }

Alternatively you can declare F a friend of G , then G::get is not needed.或者,您可以将F声明为G的朋友,则不需要G::get

I suppose your code is a stripped down example of something more complex, because in the example code there is no reason to use F::get to get the value of G::x .我想您的代码是更复杂的简化示例,因为在示例代码中没有理由使用F::get来获取G::x的值。 Once you provided a way to access the member from outside the class, you can also access it directly in main without using F::get .一旦您提供了一种从 class 外部访问成员的方法,您也可以直接在main中访问它,而无需使用F::get

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM