如何在PIMPL模式下从嵌入类中访问父类方法
How to access parent methods from embedded class in PIMPL pattern
我在这里发现了一个类似的问题,但我的意图有点不同。
类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;
}
如果您遵循头文件在一个文件而实现在另一个文件的惯例,这个问题很容易解决。
In file b.h
:
class B
{
public:
B();
~B();
private:
class A; // want to hide the implementation of A
A* impl;
};
class B::A
{
public:
A(B* _parent);
~A();
B* parent;
};
文件b.cpp
:
B::B(void)
:impl(new A(this))
{
}
//other methods and such
我的编译器给了我一个不同的错误:在B
的构造函数中,你默认构造了一个没有默认构造函数的对象(因为它是一个不完整的类)。解决方案是在类A
完全定义之后实现B
构造函数,并且头/实现分离是实现这一目标的自然方法。
将定义与声明分开:
class B
{
public:
B();
~B();
private:
class A; // want to hide the implementation of A
A* impl;
};
// define class B::A
B::B() : impl(new B::A(this)) { }
B::~B() { }
当你需要调用 B::A::A()
时,它已经被定义了。
您可以将其分成三个文件,B.hpp
, BA.hpp
和B.cpp
,您只需要将B.hpp
发送到客户端。
你就是不能。如果你想使用A,那么你必须了解A的一切。但也许还有另一种方法:
#include <iostream>
#include <string>
using namespace std;
class B
{
public:
B() : impl(newA()) {}
~B(){}
private:
static Abase *newA();
class Abase{}; // want to hide the implementation of A
Abase* impl;
friend class A;
};
在另一个文件中定义class A : B::Abase
,实现B::newA()
返回指向A
的指针。
未测试代码
相关文章:
- 如何访问由共享指针保存的类方法?
- 初始化指向类实例的智能指针并访问其方法
- 访问类成员向量最后一项的正确方法
- 使用基类对象访问派生的仅类方法
- 无法访问派生类中的基类方法
- 类变量无法从类方法访问
- 如果指针的子类的方法对于子类是唯一的,如何访问这些方法?
- 从C++中的父类实例访问子类方法
- C++ 如何使用类方法正确访问动态数组元素
- 抽象类的需求是什么?为什么要通过其基类访问派生类方法?在C++
- C++11 纯虚拟类的朋友无法访问私有方法
- 调用不访问已删除对象中的任何类成员的类方法是否安全
- C++是否提供了一种在没有范围解析运算符的情况下访问类中的类的方法?
- 为什么父类的父类方法在没有显式范围解析的情况下无法访问?
- 访问类方法中的私有指针变量C++
- 访问指针数组中的子类方法
- C - 函数能够访问类方法,而不会创建静态或对象
- 如何访问友元类方法?
- 如何在C++函数中访问类方法
- 当从不同位置访问类方法时,类成员的位置会发生变化