Say I have two classes One, the Foo class, interfaces with the user, and a Bar class that actually does stuff with any input received. Foo has an object of type Bar within it so it can call the Bar class’s method when Foo receives input from the user, passing any data it receives to the “bar::onEvent()” function for processing.
From there the Bar method, onEvent(), calls a bunch of other methods depending on what data was passed. These methods may then, in certain situations, need to call interface methods from the Foo class, like output or exit type functions.
Basically it goes, Foo gets User Input, passes that data to Bar, Bar handles that input, Bar sends data back to for, if applicable, for output. Foo handles receiving and sending data from/to the user, while Bar handles the actual processing of the data.
So Foo is constantly checking for input, when it’s received, it’s passed from Foo, to Bar to be processed. In some cases Bar may need to send new data back to Foo for output.
This is a dumbed down version of what I need to do.
class Foo
{
public:
Bar bar;
void genericFunctionSimulatingInput(char* data)
{
//when input is received, call bar class to handle the logic
while(data == someKindOfInput)
{
bar::onEvent(data);
}
}
void spitOut(char* data);
};
class Bar
{
void onEvent(char* data)
{
//do something with data
//here the modified data needs to be sent back to the user(could be a mathematical operation
//or something. But what I really need to call is "bu::spitOut(data);", not
Foo::spitOut(data);
}
};
int main()
{
//create the Foo object that I'll be using throughout
Foo bu;
//call the main loop function that will act as the heart of the code
bu.genericFunctionSimulatingInput("fdklafjdasl");
return 0;
}
I need Bar to be calling functions from the already existing Foo object that I created in the main function, which I called bu, but I have no idea how to get the bu object into the bar objects scope.
I just need the bar object within the bu object to be aware of the bu object that it resides in.
How would I go about doing this? Would I have to somehow pass in the reference, of bu, into the bar.onEvent(); method? And then to all subsequent methods called by this initial method? If so how would I go about doing this?
I understand how to do it if I had created them both within the main function. Just pass &bu, but from within bu itself? I haven’t a clue how I’d do this.
I know this is a really abstract example but I hope I’ve explained enough to where your imaginations can do the rest. If not I’ll try to better explain it.
I just need Bu’s Bar class to be able to access bu’s functions.
Maybe something like this:
The functions in
Barcalling functions inFoohave to be defined afterFoohas been properly defined. So they can’t be inline inBar.If you want the methods in Bar of Foo to be private, then make them friend of each other:
and