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 8671843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:03:16+00:00 2026-06-12T19:03:16+00:00

I have two classes: class ClassA { public: ClassB *classB; int i = 100;

  • 0

I have two classes:

class ClassA {
    public:
        ClassB *classB;
        int i = 100;
}
// and:
class ClassB {
    public:
        void longProcess();
}

I run a void from ClassB():

ClassA classA = new ClassA();
classA->i = 100;
classA->classB = new ClassB();
classB->longProcess(); // it's a long process!
// but when it will finish - I need to get the "i" variable from ClassA

How do I get the “int i” variable from the method: longProcess()? Actually, I need to running this long code in another thread that’s why I need to retrieve the “i” variable from the ClassB when the longProcess() will finished its work. Any suggestions?

Update: I try to write some code for saving the pointer to the parent class

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=[ ChildClass.h ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#include "ParentClass.h"
class ChildClass {
    public:
        ChildClass();
        ParentClass *pointerToParentClass; // ERROR: ISO C++ forbids declaration of 'ParentClass' with no type
        void tryGet_I_FromParentClass();
};

ERROR: ISO C++ forbids declaration of ‘ParentClass’ with no type

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=[ ChildClass.cpp ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#include "ChildClass.h"
ChildClass::ChildClass(){}
void ChildClass::tryGet_I_FromParentClass(){
    // this->pointerToParentClass...??? it's not work
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=[ ParentClass.h ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#include "ChildClass.h"
class ParentClass {
    public:
        ParentClass();
        ChildClass *childClass;
        int i;
};

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=[ ParentClass.cpp ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#include "ParentClass.h"
ParentClass::ParentClass(){
    childClass = new ChildClass();
    childClass->pointerToParentClass = this;
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=[ MainWindow.cpp ]-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

ParentClass *parentClass = new ParentClass();
  • 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-06-12T19:03:17+00:00Added an answer on June 12, 2026 at 7:03 pm

    You need to pass the this pointer from class A (or any pointer to it) to Class B (probably in the constructor) so that it knows where it’s contained in, and probably have a member of class B that is pointer-to-A. Then call the instance method like you would any other.

    There’s no inheritance here, so some of the other examples wouldn’t work.

    Edit: Based on your username, I’m guessing you’re more familiar with Java? What you need is a Forward Declaration. Basically this would be ChildClass.h:

    class ParentClass; // Empty
    class ChildClass
    {
        ParentClass* myParent;
        // Body omitted
    }
    

    Ensure that ChildClass.h is included at the top of ParentClass.h and declare that normally, and include ParentClass.h in both .cpp files. And ensure that all of the implementation of ChildClass is in the .cpp file, and the same for ParentClass (each in their own, or not, doesn’t matter).

    What’s happening here is that you’re creating a circular reference for the compiler, but in ChildClass.h all you need is to tell the compiler “here’s a pointer” and that’s it. Thus you don’t need the “full size” of the class, thus the “empty” forward declaration is enough. By the time the .cpp files roll around the compiler “knows” the full size of each, and doesn’t throw errors.

    See the C++ FAQ for more info on this.

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

Sidebar

Related Questions

Suppose we have two classes: class Base { private: int x; public: void f();
I have two classes: public class MultilingualString { public int Id { get; set;
Let's say we have these two classes: public class Base { public static int
Say you have two classes, order and customer: public class Customer{ public int CustomerId
I have two classes: public class TestClass1 { public int TestInt { get; set;
I have two classes: public class RichMan { public string ID {get;set;} List<Car> _cars=new
I have two different Classes: class X { public int TX1 { get; set;
I have two classes: class A { public: int i; }; class B :
I have two classes: public class Foo { public int FooId {get;set;} public virtual
I have two classes below: public class Module { public int Id { get;

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.