0

I have a class ("Class A", we'll call it) which calculates the value of a private variable (which we can call numLoaves) with a public function. I need to use the value of numLoaves in a public function in Class B.

What is the best and most efficient way to pass in this value? I'm new to programming and have been looking for a solution for hours with no luck. Note: The function in Class B is void and must be this way. I can't use global variables or objects, pointers, friend functions or friend classes. The variable must be private.

Thank you!

  • 2
    How are class A and class B associated? – Jeffery Thomas Feb 8 '14 at 14:35
2

Look at it this way. You have two objects and you need a kind of medium for them to communicate with each other.A can go and ask B what he ate for breakfast.

A can do this by saying B.WhatDidYouEatForBreakfast() or shorter B.Breakfast(). The nice thing about it is that it doesn't need to go and check it's plates and dumpster (those are private) but the question is allowed (it's public). B, on the other hand, can say what he wants (will probably say fruits and vegetables, when in fact was chips and cola), but it better be the info that you're looking for (makes sure of this in the method body).

So, basically, you'll just have to have a public method in one of the classes with a return type of the private variable. This method will return the value of the private variable. Anybody can now call that object and find out what is going on.

  • Thanks very much, but I tried doing exactly what you said (I thought it was a good explanation btw) but it is just returning "0". – user3217172 Feb 8 '14 at 20:06
  • Accidentally clicked enter, here is what I meant to say: Thanks very much, but I tried doing exactly what you said (I thought it was a good explanation btw) but it is just returning "0". I need class B to use a value from class A, so what I did was make a public function in class A called "int getNumLoaves" that returns the variable numLoaves. Class B then declares an object from Class A. Regardless of whether this object is public or private (does it matter?), however, getNumLoaves always returns 0 or something. – user3217172 Feb 8 '14 at 20:17
  • see the code from Getter and Setter Methods Maybe you missed the part that you have to initialize your variable as well? (the setMyVar function in the above example – mihai Feb 8 '14 at 20:21
0

In some way this will boil down to class A setting the value in class B.

Class B should have it's own private copy of numLoaves and use a setter to set it.

void ClassB::setNumLoaves(const NumLoaves_t& numLoaves)
{
    _numLoaves = numLoaves;
}

Class A should have a method which will update class B.

void ClassA::updateClassB(ClassB& classB)
{
    classB.setNumLoaves(_numLoaves);
}

Finally, in whatever way class A and class B are associated, class A updates class B.

classA.updateClassB(classB);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.