0
//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. beta is a private variable in nested class. I want to access the updated value of beta in GetVariable method.

varconst
  • 15
  • 6
  • 2
    What is your question? – 273K Jun 11 '21 at 17:11
  • I want to access the variable "beta" which is being set in "SetVariable" method, in the "GetVariable" method. @S.M. – varconst Jun 11 '21 at 17:15
  • It's a `private` member variable. Make it `public` or `protected`, or write a `public` or `protected` accessor, or declare `C` to be a `friend` of `B`. ETA: Oops, I thought `C` was actually relevant to this question. Apparently it's not and this is talking about access from `A`. – Nathan Pierson Jun 11 '21 at 17:16
  • 1
    `GetVariable` is a member function of the class `A`. The class `A` doesn't have any members named `beta`; in fact it has no member data at all. So there is no way that the function can get at `beta`. – Pete Becker Jun 11 '21 at 17:16
  • 2
    you have three classes. `C` inherits from `B` and has all members that `B` has. They are both declared inside `A`, but `A` has no other relation to them than that. In your `main` you create a `C` instance, but there is no `A` instance – 463035818_is_not_an_ai Jun 11 '21 at 17:17
  • 1
    Look, part of the ongoing confusion here is in the nesting of the classes, which adds nothing to the design here. Just define class `B` outside of `A`, then define `C` outside of `A`, then define class `A`. Sort out who does what with which and to whom, and then, if you really want to continue with this muddle, put `B` and `C` back inside `A`. – Pete Becker Jun 11 '21 at 17:18
  • 2
    what is the actual problem you are trying to solve here? As Pete mentioned, if you cannot solve it without nesting the classes then there is something wrong in your understanding and nesting the classes wont help either – 463035818_is_not_an_ai Jun 11 '21 at 17:19
  • @NathanPierson I cannot make it public. I want to know a workaround. – varconst Jun 11 '21 at 17:22
  • @PeteBecker "beta" is located as a private variable in class B. Is there a way to first get the variable to a public method in A and then transfer it to a private method in A? – varconst Jun 11 '21 at 17:24
  • `private` is close to being irrelevant here. The issue is that an `A` has no member `beta` not inherited and not as a member. The only way to access the `beta` member of a `C` instance from `A`s `GetVariable` is to add a `C` member to `A`, or to pass a `C` to the method – 463035818_is_not_an_ai Jun 11 '21 at 17:29
  • Show this in the code. You have no instances of A, no clue how you call GetVariable. – 273K Jun 11 '21 at 17:29
  • @varconst This question really needs more clarity. It's hard to tell what you want to do, why the most direct route to doing so isn't available to you, and how much the answers to those first two questions are based on incorrect understandings you have. (For instance, you appear to think that an `A` instance will contain a `B` instance somewhere. This is not the case.) Nobody can help you if we can't tell what you're trying to do or what may or may not invalidate otherwise functional solutions. – Nathan Pierson Jun 11 '21 at 17:29
  • 1
    there is an elephant in the room, and I am not sure if you understand what comments are trying to tell. Do you understand why `struct Foo { struct bar{ int x; }; int foo_get() { return x; } };` is wrong and makes no sense? – 463035818_is_not_an_ai Jun 11 '21 at 17:31
  • well, I already said it, `private` is not the issue, it is close to being irrelevant for your question. You could add a getter to access it, but thats not the problem – 463035818_is_not_an_ai Jun 11 '21 at 17:37
  • If I make A a friend of B, shouldn't A be able to access the variable "beta" in it's public method if not private? – varconst Jun 11 '21 at 17:37
  • I understand that "beta" is not accessible to class A. But in order to get the value of "beta" in class A, can I not do something like, get a class pointer of B in A somewhere and then access that variable? Some getter method may be? – varconst Jun 11 '21 at 17:40
  • @varconst sure you can add a member to `A` or pass an object to the `GetVariable` as I did in the answer – 463035818_is_not_an_ai Jun 11 '21 at 17:44
  • @varconst -- forget `friend`, `public`, `private`. class `A` does not have a member named `beta`, and it has no member variables, so there is nothing in it that holds a `beta`. As I said before, remove that nesting; it's just noise. Get the code to work. Then add the complications if you still want to. – Pete Becker Jun 11 '21 at 17:54
  • @PeteBecker I found this online and I am interested in implementing this but not sure how to do that in my code. "The access to private data members outside of class is allowed only to friend classes or functions. But you can specify a special function Accessor to get the value of private data member." – varconst Jun 11 '21 at 18:00
  • @varconst better dont try to learn from random code you find online. You can write a `getter` and you can make another class a `friend`, but to some extend that defeats the purpose of making the member `private` in the first place. You will have a hard time to really understand what stuff is good for if you only study the solutions / workarounds without knowing what they are good for / when to apply them / what problem they are actually solving. – 463035818_is_not_an_ai Jun 11 '21 at 18:19

1 Answers1

3

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.

Your code looks a little complicated because it has inheritance and what not. 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() ?

You can't.

What you can do is...

Add a G member to F and return the member of that instance:

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

or pass a G to 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.


Actually also the nesting is not that relevant because with regards to accessing the member G::x it is similar to, now with 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. Second, you cannot access it when it is private. As above, we pass an instance to get and in addition add an accessor:

 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.

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. 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.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • struct F { struct G { int x; }; int get(const G& g) { return g.x; } }; Yes, i think this is what I had in mind. But when I tried in class, it still says "x" is private within its context. Can you edit this method in my code? When I call "get" method, how do I pass the class in that? get(G) right? – varconst Jun 11 '21 at 17:46
  • @varconst in my answer and in your comment everything is public. If you want to access a private you need to add an accessor – 463035818_is_not_an_ai Jun 11 '21 at 17:47
  • Okay so how to add an accessor? Can you edit in my code? – varconst Jun 11 '21 at 17:48
  • 1
    @varconst Consider trying to read [a basic C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) because at this point some of these questions are _very_ basic. Trying to teach yourself programming by looking at random code snippets you found online, making random changes, and seeing what compiles is not going to be very effective. – Nathan Pierson Jun 11 '21 at 18:01
  • @varconst see edit. Note that the answer was written to try to unravel a misunderstanding. It is still unclear whats the actual problem you are trying to solve. My "solution" in the last part compiles and does what you ask for, but it is unclear why you would need something like this in the first place. – 463035818_is_not_an_ai Jun 11 '21 at 18:16