-2

Hi I'm having some trouble understanding the friend keyword from C++

Let's say i have a class as shown below:

class A
{
private:
    friend class B;
    struct Astruct
    {
        int funVar = 1;
    };
    Astruct myStruct
public:
    changeAStruct(); // changes the value of funVar to 2
};

class B
{
    //how do i here get access to myStruct with the value 2?
};

Hopefully the pseudo code above will show you my problem. I want the same instance of myStruct that using the this pointer would get me in class A in class B. How can I achieve this?

What I don't want is typing this in class B:

A::Astruct myStruct

As this would create a new struct in class B with the funVar being set to 1. I want the same struct in class A, but now in class B...

EDIT: I suppose that from main i can send the myStruct into class B as a reference and access it from there. Is this an optimal thing to do?

mrmagin
  • 61
  • 1
  • 7
  • 2
    If you want the functionality of a pointer, use a pointer. – Mr Lister May 13 '18 at 08:27
  • `this` refers to the `B` instance inside `B`'s scope. So where is that other `A` instance you want to operate on? – StoryTeller - Unslander Monica May 13 '18 at 08:27
  • but how would that pointer look? simply typing A::Astruct *myStruct does not really help – mrmagin May 13 '18 at 08:37
  • A friend can access things, but it can't access things that don't exist. Code in `B` can access the type `Astruct`. And code in `B` that has an `A` instance to work with can access the `myStruct` attribute of that `A` instance. But there is no `myStruct` on the `A` class itself, so there's nothing for that `B` code to access. – abarnert May 13 '18 at 08:45
  • "does not really help" - Why not? What goes wrong if you try it? We probably need a little more context to answer this... – hlt May 13 '18 at 08:45
  • Until you can get the hang of class vs. instance members, you probably need to step back and not worry about access and friendship. First design what you want with everything public. Then, once you have something that makes sense and works, you can ask how to change it to make things private and share them (if you can't figure it out yourself). – abarnert May 13 '18 at 08:47
  • Also take a look at: https://stackoverflow.com/questions/17434/when-should-you-use-friend-in-c – sɐunıɔןɐqɐp May 13 '18 at 08:48
  • On re-reading the question, it seems to me that what you need is for B to be a derived class of A. – Mr Lister May 13 '18 at 09:03
  • "I want the same instance of myStruct that using the this pointer would get me in class A in class B.": this sentence is a incomprehensible. Please edit the question and rewrite it in a more correct english and explain better. – roschach May 13 '18 at 11:36

1 Answers1

0

I am not sure to understand what you want, but you could do something like that:

class A
{
private:
    friend class B;
    struct Astruct
    {
        int funVar = 1;
    };
    Astruct myStruct;
};

class B
{
public:
    int getFunVar(A &a)
    {
        return a.myStruct.funVar;
    }
};
benjarobin
  • 4,410
  • 27
  • 21
  • well it's pretty much this execpt i need a struct in class B to be the same struct as in class A. Class A changes the structs and then transfers it to class B – mrmagin May 13 '18 at 09:38
  • Actually, the thing i wonder most is if it's possible of doing your exampel without having to call getFunVar? can i somehow in class B get funVar without function calls? – mrmagin May 13 '18 at 09:56
  • @mrmagin • `a.myStruct.funVar` is getting `funVar` without function calls. So, yes, it is possible. – Eljay May 13 '18 at 11:06