Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7416987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:36:33+00:00 2026-05-29T07:36:33+00:00

Say I have two classes One, the Foo class, interfaces with the user, and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:36:33+00:00Added an answer on May 29, 2026 at 7:36 am

    Maybe something like this:

    class Foo;
    
    class Bar
    {
    public:
        Bar(Foo &foo)
            : m_foo(foo)
            { }
    
        void onEvent();
    
    private:
        Foo &m_foo;
    };
    
    class Foo
    {
    public:
        Foo()
            : m_bar(*this)
            { }
    
    
        void onEvent()
            {
                m_bar.onEvent();
            }
    
        void onEventDone()
            {
            }
    
    private:
        Bar m_bar;
    };
    

    The functions in Bar calling functions in Foo have to be defined after Foo has been properly defined. So they can’t be inline in Bar.

    void Bar::onEvent();
    {
        // Do stuff
        m_foo.onEventDone();
    }
    

    If you want the methods in Bar of Foo to be private, then make them friend of each other:

    class Bar
    {
        friend class Foo;
        // ...
    

    and

    class Foo
    {
        friend class Bar;
        // ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two models, Classes and People. A Class might have one
Let's say I have two classes. Each class has one parameter. Parameter of first
So let's say I have two classes that inherit a base class that has
Let's say I have two classes that look like this: public class ByteFilter {
I have got two classes, one a subclass of the other (say Animal and
Say I have two classes and have a requirement that the primary key property
Say I have two classes, and neither of them are GUI components. Class A
Let's say we have two classes, Foo and Foo Sub, each in a different
Say I have two separate classes, A and B. I also have Repository class
Let's say we have these two classes: public class Base { public static int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.