子級訪問父類我有兩個類:C++:從
class ClassA {
public:
ClassB *classB;
int i = 100;
}
// and:
class ClassB {
public:
void longProcess();
}
我從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
我如何得到「INT I 「從方法變量:longProcess()?實際上,我需要在另一個線程中運行這個長代碼,這就是爲什麼我需要在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();
};
ERROR:ISO C++禁止 '父類' 的聲明無類型
// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = [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;
};
// - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = = = - = - = - = - = - = - = - = - = - = - =
#include "ParentClass.h"
ParentClass::ParentClass(){
childClass = new ChildClass();
childClass->pointerToParentClass = this;
}
// - = - = - = - = - = - = - = - = - = - = - = = - = - = - = - = - = - = - = [MainWindow.cpp] - = - = - = - = - = - = - = - = = - = - = - =
ParentClass *parentClass = new ParentClass();
*除了一個字母的大寫之外,不要給變量指定與班級同名的變量。它會讓人想要打你。像我這樣的人。 – Beta
@slashmais,對不起,這是一個錯誤。它實際上是同一班。我糾正了它。 – JavaRunner
您似乎在ChildClass.h和ParentClass.h中聲明瞭ParentClass。這是打算嗎? –