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

  • SEARCH
  • Home
  • 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 6214897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:57:26+00:00 2026-05-24T06:57:26+00:00

This is more of a general programming question so the code examples I give

  • 0

This is more of a general programming question so the code examples I give will just be pseudo-code. I program in C++, Java, and Python so the pseudo-code is a mix of those. I am not too sure what the name of this is called so if you could give me a name for this, I can Google for more information about it I would greatly appreciate it.

Let’s say I have a class called A. In this class, I create an instance of a class called B:

class A {
    //instance variables
    classB;
    variable1;
    variable2;

    //instance methods
    instanceFunction(parameter1, parameter2) {
        //Do Stuff
    }

    function1(parameter1) {
        classB = new B(some parameters);
    }

    setVariable1(value) {
        variable1 = value;
    }

    getVariable2() {
        return variable2;
    }
}

In class B, I want to make changes to or make use of instance variables in class A. I can do this by passing a reference to A into B. So my A::function1 would look like this:

function1(parameter1) {
    variable1 = new B(this, other parameters);
    //Python syntax:
    //variable1 = B(self, other parameters)
}

and my class B would look like this:

class B {
    //instance variables
    parentClass;
    variable2;

    //instance methods
    instanceFunction(classA, other parameters) {
        parentClass = classA;
    }

    function1() {
        parentClass.setVariable1(someValue);
    }

    function2() {
        variable2 = parentClass.getVariable2();
    }
}

What other ways are there to have use of the variables in class A inside of class B?

If the code was C++ or Java, assume all variables are private and all methods and functions are public. Also assume all variables are passed by references or a pointers.

Edit:

First, thanks for all the responses!

Some clarification:

The reason I am asking this question is I have done a good amount of Qt programming in C++ and Python. In Qt, there are signals and slots. This lets inner objects tell the outer object to do something. For example I have an object A with objects B and C inside of it. Some change happens to object B and B needs to let A and C know. So B will emit a signal. A will catch this signal and do what it needs to do. Then, A will also let C know that B emitted this signal. This way C can do what it needs to do.

The reason I asked my question is because I was wondering how I can do something like I described without using the Qt libraries.

Also, let’s assume B “is not” an A so I can’t/don’t want to use inheritance.

  • 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-24T06:57:28+00:00Added an answer on May 24, 2026 at 6:57 am

    I am not too sure what the name of this is called…

    I am having trouble following your pseudo-code, but you might be able to accomplish what you’re looking for via:

    • Inheritance, which allows derived classes to access protected variables from a base class (static or instance variables)
    • Friend functions (C++) (which allows functions to have access to private instance variables on a class)
    • Dependency Injection (But probably only if you have more complex requirements than you’re actually stating in your question. In this super-simple case, you’d just be accessing public properties or fields when an instance is passed in to a function – you might have to access them through a public getter/setter, since you want the variables to be private)

    Edit:

    After your edits, it is pretty clear that you want the Observer Design Pattern. This allows you to decouple the code that responds to a state change from the code that signals the state change.

    That pattern is less about access to variables (as my first links were about), than it is about responding to events (or “state transitions”, if you think about your class as a Finite State Machine).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Although I'm coding in Objective C, this is more of a general programming question.
This is more of a maths/general programming question, but I am programming with PHP
Although I'm programming in C++, this is more of a general question about design.
This is more a general purpose programming question than language specific. I've seen several
This is more a general question but my particular case involves a ruby/rails app
I suppose this question is more general than working with COM components. I have
this may be a more general opengl question. using OpenGL ES for 2d, and
I know this SO question, but it deals with the subject in more general
This is more an observation than a real question: MS-Access (and VBA in general)
This is less an HTML, CANVAS question and more of a general math question.

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.