0

I have a problem, and I am almost certain I can find a solution by restructuring the code I have to eliminate the problem, but I was wondering if there was a way to achieve the same result with what I have right now.

Suppose we have class A:

class A
{
public:
    int thing;
    void dostuff();
    std::list<B> mList;
}

void A::doStuff()
{
    for(auto x : mList)
        x.doMoreStuff();
}

And then there is class B.

class B
{
    void doMoreStuff();
}

Is it possible in any way to make B's doMoreStuff change the 'int thing' variable of class A without passing the instance of class A to B or similar convoluted methods?

9
  • Yeah, I was hoping to do it without that, or a superclass or something of that sort. Commented Mar 16, 2017 at 8:10
  • 1
    How should B know about A if it doesn't get a reference or pointer to A? Commented Mar 16, 2017 at 8:11
  • What is B.doMoreStuff(); supposed to mean? Commented Mar 16, 2017 at 8:12
  • 2
    You could go down the 'static' route but then you'll be limited to a single variable 'thing' amongst all other instances of A Commented Mar 16, 2017 at 8:13
  • @juanchopanza Sorry about the confusion, I was in a hurry. Presumably it would be the instance of class B, also referred to as 'B'. I probably should edit that, and make it clearer. Commented Mar 16, 2017 at 8:20

3 Answers 3

2

If you only want to change the value of a certain instance you'll have to reference.

Tho something you could do is use the return value of the method in B

class A
{
    int thing;
    B b;
    void dostuff() {
        thing += b.doMoreStuff();
    };
}

class B
{
    int doMoreStuff() {
       return 1;
    };
}

Or you can make either int thing or void doMoreStuff() a static so it can be accessed from class memory instead of instance memory like so:

class A
{
    static int thing;

    void dostuff() {
        B.DoMoreStuff();
    };
}


class B
{
    static void doMoreStuff() {
       A.thing += 2;
    };
}
Sign up to request clarification or add additional context in comments.

1 Comment

There is an error in your code. you can't do B b = new B(); it's either B * b = new B(); or B b; and if you choose to do the first option with pointers you should also change b.doMoreStuff(); to b->doMoreStuff();
1

Yes you can by simply passing the address of the int thing of A for example:

B.doMoreThing(&myInt);

and the definition of doMoreThing would be: doMoreThing(int * myInt) and when modifying the value of the int inside the function you should do for example *myInt+=5 the * is important to access the variable.

6 Comments

I guess that's the best I am going to get. I really hoped there was a way to do it without references -- for no particular reason.
I don't think so. I guess that this is the best way to do it. I don't really like the solutions making the variable static because it becomes accessible by everyone and everywhere
I switched the answer to the static one as that one achieves the same result without the reference. I don't like the static either, but in my case, doMoreStuff() has about 5 lines of inputs, having to put a reference in just makes things a hell of a lot more complicated.
No problem but take care because declaring a variable as static makes it accessible from everywhere but also makes instantiated once which means that if you do A a; a.thing=9 and then do A a2; a2.thing=46 that will also change the a.thing and makes it equal 46
I know, that's fine. I only need one a.thing-- it's practically a global variable anyway. This saves me the trouble of having to pass it by reference to other things.
|
0

You're not going to be able to write to thing without a reference or a pointer to it. That said, thing is private anyway, so you'd need a setter. Alternatively you could make B a friend of A, or give B a pointer to member to thing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.