私には2つのクラスがあります:
class ClassA {
public:
ClassB *classB;
int i = 100;
}
// and:
class ClassB {
public:
void longProcess();
}
ClassB()からvoidを実行します。
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
メソッドlongProcess()から「inti」変数を取得するにはどうすればよいですか?実際、この長いコードを別のスレッドで実行する必要があるため、longProcess()の作業が終了したときに、ClassBから「i」変数を取得する必要があります。助言がありますか?
更新:親クラスへのポインターを保存するためのコードを書こうとしています
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=[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();
};
エラー:ISO C ++は、タイプのない「ParentClass」の宣言を禁止しています
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=[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();