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();
You need to pass the
thispointer 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:Ensure that
ChildClass.his included at the top ofParentClass.hand declare that normally, and includeParentClass.hin both .cpp files. And ensure that all of the implementation ofChildClassis in the .cpp file, and the same forParentClass(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.hall 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.