2012-10-10 13 views
2

子級訪問父類我有兩個類: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(); 
+0

*除了一個字母的大寫之外,不要給變量指定與班級同名的變量。它會讓人想要打你。像我這樣的人。 – Beta

+0

@slashmais,對不起,這是一個錯誤。它實際上是同一班。我糾正了它。 – JavaRunner

+0

您似乎在ChildClass.h和ParentClass.h中聲明瞭ParentClass。這是打算嗎? –

回答

5

您需要將類A的指針(或其指向它的任何指針)傳遞給類B(可能在構造函數中),以便它知道它包含在哪裏,並且可能具有指向類B的成員-to-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欲瞭解更多信息。

+0

這是一個很好的解決方案。試着去做,但是我的代碼因爲錯誤而失敗了我已經更新了第一條消息 – JavaRunner

+0

謝謝你的鏈接,你真的幫助我了!我嘗試做一些睾丸 - 現在它已經工作了!這正是我一直在尋找的! – JavaRunner

0

在你的孩子的方法結束時調用父類的方法:

void ClassB::longProcess() { 
    // do your processing 
    // ... 

    callMeWhenLongProcessHasFinished(); 
} 

如果,理由很奇怪,你不能修改longProcess,寫一個順序調用兩種方法的另一種方法。

0

您應該在ClassB上使用Join,以便ClassA在ClassB完成之前不會執行它的方法。或者,只需在ClassB的方法結束時從ClassA調用該方法即可。

+0

但是,如果我在另一個線程中運行ClassB,我怎樣才能從ClassA調用方法? – JavaRunner

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)); 
} 

獲取調用在不同的線程正在做將需要以適當的線程安全的方式發送對象,但這是一個完全獨立的問題。

+0

我不能傳遞一個指針......我不知道爲什麼,但它失敗了,並帶有錯誤:)(源代碼在第一條消息中可用) – JavaRunner

+0

如果類型未知,則需要轉發聲明它。由於您在設置中獲得了循環依賴關係,因此您需要轉發至少一個指針。不過,你應該避免循環依賴。傳遞一個'std :: function '很好地打破了依賴循環。 –