我發現了類似的問題here,但我的意圖有點不同。如何從PIMPL模式的嵌入類訪問父方法
B類是嵌入類,而A類是嵌入類。我想讓B :: A有權訪問B類的成員函數。我通過g ++(Ubuntu/Linaro 4.5.2-8ubuntu4)看到了編譯錯誤4.5.2。詳細的錯誤如下:
~/Documents/C++ $ g++ embed.cpp
embed.cpp:5:7: error: ‘B’ has not been declared
embed.cpp: In constructor ‘B::B()’:
embed.cpp:10:27: error: invalid use of incomplete type ‘struct B::A’
embed.cpp:14:9: error: forward declaration of ‘struct B::A’
有沒有一種方法可以讓它工作? 謝謝
#include <iostream>
#include <string>
using namespace std;
class B
{
public:
B() : impl(new B::A(this)) {}
~B(){}
private:
class A; // want to hide the implementation of A
A* impl;
};
class B::A
{
public:
A(B* _parent) : parent(_parent) {} // let embedded class A has access to this parent class
~A() { parent = NULL; }
B* parent;
};
int main(void)
{
return 0;
}