具有继承性的 Pimpl 习语
我想将 pimpl 习惯用法与继承一起使用。
这是基公共类及其实现类:
class A
{
public:
A(){pAImpl = new AImpl;};
void foo(){pAImpl->foo();};
private:
AImpl* pAImpl;
};
class AImpl
{
public:
void foo(){/*do something*/};
};
我希望能够创建派生公共类及其实现类:
class B : public A
{
public:
void bar(){pAImpl->bar();}; // Can't do! pAimpl is A's private.
};
class BImpl : public AImpl
{
public:
void bar(){/*do something else*/};
};
但我不能在 B 中使用 pAimpl,因为它是 A 的私有类。
所以我看到了一些解决它的方法:
- 在 B 中创建 BImpl* pBImpl 成员,并使用附加的 A 构造函数 A(AImpl*) 将其传递给 A。
- 将 pAImpl 更改为受保护(或添加 Get 函数),并在 B 中使用它。B
- 不应继承自 A。在 B 中创建 BImpl* pBImpl 成员,并在 B 中创建 foo() 和 bar(),这将使用pBImpl。
- 还有其他办法吗?
我应该选择什么?
I want to use pimpl idiom with inheritance.
Here is the base public class and its implementation class:
class A
{
public:
A(){pAImpl = new AImpl;};
void foo(){pAImpl->foo();};
private:
AImpl* pAImpl;
};
class AImpl
{
public:
void foo(){/*do something*/};
};
And I want to be able to create the derived public class with its implementation class:
class B : public A
{
public:
void bar(){pAImpl->bar();}; // Can't do! pAimpl is A's private.
};
class BImpl : public AImpl
{
public:
void bar(){/*do something else*/};
};
But I can't use pAimpl in B because it is A's private.
So I see some ways to solve it:
- Create BImpl* pBImpl member in B, and pass it to A with additional A constructor, A(AImpl*).
- Change pAImpl to be protected (or add a Get function), and use it in B.
- B shouldn't inherit from A. Create BImpl* pBImpl member in B, and create foo() and bar() in B, that will use pBImpl.
- Any other way?
What should I choose?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为从纯粹面向对象理论的角度来看,最好的方法是不要让BImpl继承AImpl(这就是你在选项3中的意思吗?)。 然而,让 BImpl 派生自 AImpl(并将所需的 impl 传递给 A 的构造函数)也是可以的,前提是 pimpl 成员变量是 const。 使用 get 函数还是直接从派生类访问变量并不重要,除非您想在派生类上强制执行 const 正确性。 让派生类更改 pimpl 不是一个好主意 - 它们可能会破坏 A 的所有初始化 - 并且让基类更改它也不是一个好主意。 考虑您的示例的扩展:
虽然您可能没有 swap() 函数,但您可以很容易地想到会发生类似的问题,特别是如果 A 是可分配的,无论是偶然还是有意的。 这有点微妙地违反了里氏可替代性原则。 解决方案是:
构建后不要更改 pimpl 成员。 将它们声明为
AImpl * const pimpl
。 然后,派生构造函数可以传递适当的类型,并且派生类的其余部分可以放心地向下转型。 但是,您就不能进行非抛出交换、赋值或写时复制等操作,因为这些技术要求您可以更改 pimpl 成员。 然而,如果您有继承层次结构,您可能并不真正打算执行这些操作。分别为 A 和 B 的私有变量提供不相关(且愚蠢)的 AImpl 和 BImpl 类。 如果B想要对A做某事,那么使用A的公共或受保护的接口。 这也保留了使用 pimpl 的最常见原因:能够将 AImpl 的定义隐藏在派生类无法使用的 cpp 文件中,因此当 A 的实现发生更改时,您的一半程序不需要重新编译。
I think the best way from a purely object-oriented-theoretical perspective is to not make BImpl inherit from AImpl (is that what you meant in option 3?). However, having BImpl derive from AImpl (and passing the desired impl to a constructor of A) is OK as well, provided that the pimpl member variable is
const
. It doesn't really matter whether you use a get functions or directly access the variable from derived classes, unless you want to enforce const-correctness on the derived classes. Letting derived classes change pimpl isn't a good idea - they could wreck all of A's initialisation - and nor is letting the base class change it a good idea. Consider this extension to your example:Although you might not have a swap() function, you can easily conceive of similar problems occurring, particularly if A is assignable, whether by accident or intention. It's a somewhat subtle violation of the Liskov substitutability principle. The solutions are to either:
Don't change the pimpl members after construction. Declare them to be
AImpl * const pimpl
. Then, the derived constructors can pass an appropriate type and the rest of the derived class can downcast confidently. However, then you can't e.g. do non-throwing swaps, assignments, or copy-on-write, because these techniques require that you can change the pimpl member. However however, you're probably not really intending to do these things if you have an inheritance hierarchy.Have unrelated (and dumb) AImpl and BImpl classes for A and B's private variables, respectively. If B wants to do something to A, then use A's public or protected interface. This also preserves the most common reason to using pimpl: being able to hide the definition of AImpl away in a cpp file that derived classes can't use, so half your program doesn't need to recompile when A's implementation changes.
我会做(1),因为A的私有对B来说无关紧要。
实际上我不会像你建议的那样将它传递给A,因为A在A::A()中创建了自己的私有私有。
从 Bis 调用
pApimpl->whatever()
也不合适(私有意味着私有)。I would do (1) because A's privates are or no business for B.
Actually I would not pass it to A as you suggest, because A makes its own in A::A().
Calling
pApimpl->whatever()
from Bis also not appropriate (private means private).正如 stefan.ciobaca 所说,如果您确实希望 A 可扩展,您就会希望
pAImpl
受到保护。但是,您在
void bar(){pAImpl->bar();};
的B
中的定义似乎很奇怪,因为bar
是BImpl 上的方法,而不是 AImpl 上的方法。至少有三种简单的替代方案可以避免该问题:
BImpl
扩展AImpl
(继承foo
的现有实现,而不是定义另一个实现),BImpl< /code> 定义了
bar
,而B
使用其私有的BImpl* pBImpl
来访问两者。B
持有指向AImpl
和BImpl
的私有指针,并转发foo
和>bar
给适当的实施者。As stefan.ciobaca said, if you really wanted A to be extendable, you'd want
pAImpl
to be protected.However, your definition in
B
ofvoid bar(){pAImpl->bar();};
seems odd, asbar
is a method onBImpl
and notAImpl
.There are at least three easy alternatives that would avoid that issue:
BImpl
extendsAImpl
(inheriting the existing implementation offoo
rather than defining another),BImpl
definesbar
, andB
uses its privateBImpl* pBImpl
to access both.B
holds private pointers to each ofAImpl
andBImpl
and forwards each offoo
andbar
to the appropriate implementer.正确的做法是(2)。
一般来说,您可能应该考虑将所有成员变量默认设置为受保护而不是私有。
大多数程序员选择 private 的原因是他们不考虑其他人想要从他们的类中派生,并且大多数介绍性 C++ 手册都教授这种风格,从某种意义上说,所有示例都使用 private。
编辑
代码重复和内存分配是使用 pimp 设计模式的不良副作用,据我所知无法避免。
如果您需要让 Bimpl 继承 Aimpl 并且希望通过 A 和 B 向它们公开一致的接口,则 B 还需要继承 A。
在这种情况下,您可以做的一件事是简化事情,即让 B 继承 A 并只需更改构造函数,以便 B::B(...) {} 创建 Bimpl,并为 Aimpl 中不存在的 Bimpl 的所有方法添加调度。
The correct way is to do (2).
In general, you should probably consider to make all you member variables protected by default instead of private.
The reason most programmers choose private is that they don't think about others who want to derive from their class and most introductory C++ manuals teach this style, in the sense that all the examples use private.
EDIT
Code duplication and memory allocation are undesired side-effects of using the pimp design pattern and cannot be avoided to my knowledge.
If you need to have Bimpl inherit Aimpl and you want to expose a consistent interface to them through A and B, B would also need to inherit A.
One thing you can do to simplify things in this scenario is to have B inherit from A and only change the contructor such that B::B(...) {} creates a Bimpl, and add dispatches for all methods of Bimpl that are not in Aimpl.