我有两个班级:
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();
};
错误: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();
答案 0 :(得分:7)
您需要将类{A(或任何指向它)的this
指针传递给B类(可能在构造函数中),以便它知道它包含在哪里,并且可能有B类成员这是指向A的指针。然后像调用其他任何方法一样调用实例方法。
这里没有继承,所以其他一些例子也行不通。
编辑:根据您的用户名,我猜你更熟悉Java?您需要的是前瞻性声明。基本上这将是ChildClass.h
:
class ParentClass; // Empty
class ChildClass
{
ParentClass* myParent;
// Body omitted
}
确保ChildClass.h
的顶部包含ParentClass.h
并正常声明,并在两个.cpp文件中包含ParentClass.h
。并确保ChildClass
的所有实现都在.cpp文件中,而ParentClass
的相同内容(每个都在他们自己,或不在,并不重要)。
这里发生的是你正在为编译器创建一个循环引用,但在ChildClass.h
中你需要的只是告诉编译器“这里是一个指针”就是这样。因此,您不需要该类的“完整大小”,因此“空”前向声明就足够了。当.cpp文件滚动时,编译器“知道”每个文件的完整大小,并且不会抛出错误。
有关详细信息,请参阅C++ FAQ。
答案 1 :(得分:1)
如果A::callMeWhenLongProcessFinished()
实际上是您希望从B::longProcess()
调用的唯一函数,则可以将指向您的A
对象的指针传递给函数:
void B::longProcess(A* object) {
// long process
object->callMeWhenLongProcessFinished();
}
如果设置不是那么简单,您可以让B::longProcess()
取一个std::function<void()>
设置为调用正确的函数并调用它:
void B::longProcess(std::function<void()> callback) {
// long process
if (callback) {
callback();
}
}
void some_function() {
A* aptr = get_A_object();
B* bptr = get_B_object();
bptr->longProcess(std::bind(&A::callMeWhenLongProcessFinished, aptr));
}
在不同的线程中完成调用将需要以适当的线程安全的方式发送对象,但这是一个完全独立的问题。
答案 2 :(得分:0)
在子方法结束时调用父方法:
void ClassB::longProcess() {
// do your processing
// ...
callMeWhenLongProcessHasFinished();
}
如果由于某些奇怪的原因,您无法修改longProcess
,请编写另一种串行调用这两种方法的方法。
答案 3 :(得分:0)
你应该在ClassB上Join,以便ClassA在ClassB完成之前不会执行它的方法。或者,只需在ClassB的方法结束时从ClassA调用该方法。