我有一個一流的設計問題,可能與這個例子簡化:C++:父指針類,包含孩子
// foo.h
#include "foo2.h"
class foo
{
public:
foo2 *child;
// foo2 needs to be able to access the instance
// of foo it belongs to from anywhere inside the class
// possibly through a pointer
};
// foo2.h
// cannot include foo.h, that would cause an include loop
class foo2
{
public:
foo *parent;
// How can I have a foo pointer if foo hasn't been pre-processed yet?
// I know I could use a generic LPVOID pointer and typecast later
// but isn't there a better way?
};
難道還有比使用通用指針或父指針傳遞給其它任何其他方式foo2會員的每次電話會議?
我還沒做C++。但似乎你必須將父指針傳遞給你的類。您可以創建一個名爲parent的屬性併爲其分配父指針。 – Frank
@Frank:問題是編譯器中的循環依賴。傳遞父指針並不是這裏棘手的部分。 –