C++ Pimpl 习惯用法 vs 纯虚拟类接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/825018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:28:17  来源:igfitidea点击:

Pimpl idiom vs Pure virtual class interface

c++abstract-classpimpl-idiom

提问by Arkaitz Jimenez

I was wondering what would make a programmer to choose either Pimpl idiom or pure virtual class and inheritance.

我想知道是什么让程序员选择 Pimpl 惯用语或纯虚拟类和继承。

I understand that pimpl idiom comes with one explicit extra indirection for each public method and the object creation overhead.

我知道 pimpl 成语为每个公共方法和对象创建开销提供了一个明确的额外间接。

The Pure virtual class in the other hand comes with implicit indirection(vtable) for the inheriting implementation and I understand that no object creation overhead.
EDIT: But you'd need a factory if you create the object from the outside

另一方面,纯虚拟类带有用于继承实现的隐式间接(vtable),我知道没有对象创建开销。
编辑:但是如果您从外部创建对象,则需要一个工厂

What makes the pure virtual class less desirable than the pimpl idiom?

是什么让纯虚拟类比 pimpl 习语更不受欢迎?

采纳答案by Paul Hollingsworth

When writing a C++ class, it's appropriate to think about whether it's going to be

写C++类的时候,适当的考虑一下它是否会被

  1. A Value Type

    Copy by value, identity is never important. It's appropriate for it to be a key in a std::map. Example, a "string" class, or a "date" class, or a "complex number" class. To "copy" instances of such a class makes sense.

  2. An Entity type

    Identity is important. Always passed by reference, never by "value". Often, doesn't make sense to "copy" instances of the class at all. When it does make sense, a polymorphic "Clone" method is usually more appropriate. Examples: A Socket class, a Database class, a "policy" class, anything that would be a "closure" in a functional language.

  1. 值类型

    按值复制,身份从不重要。它适合作为 std::map 中的键。例如,“字符串”类、“日期”类或“复数”类。“复制”这样一个类的实例是有道理的。

  2. 实体类型

    身份很重要。总是通过引用传递,从不通过“值”。通常,“复制”类的实例根本没有意义。如果确实有意义,多态“克隆”方法通常更合适。例子:一个 Socket 类,一个数据库类,一个“策略”类,任何在函数式语言中都是“闭包”的东西。

Both pImpl and pure abstract base class are techniques to reduce compile time dependencies.

pImpl 和纯抽象基类都是减少编译时间依赖性的技术。

However, I only ever use pImpl to implement Value types (type 1), and only sometimes when I really want to minimize coupling and compile-time dependencies. Often, it's not worth the bother. As you rightly point out, there's more syntactic overhead because you have to write forwarding methods for all of the public methods. For type 2 classes, I always use a pure abstract base class with associated factory method(s).

但是,我只使用 pImpl 来实现值类型(类型 1),并且只有在我真正想要最小化耦合和编译时依赖性的时候才使用。通常,这是不值得的。正如您正确指出的那样,存在更多的语法开销,因为您必须为所有公共方法编写转发方法。对于类型 2 类,我总是使用带有关联工厂方法的纯抽象基类。

回答by D.Shawley

Pointer to implementationis usually about hiding structural implementation details. Interfacesare about instancing different implementations. They really serve two different purposes.

Pointer to implementation通常是关于隐藏结构实现细节。Interfaces是关于实例化不同的实现。它们确实有两个不同的目的。

回答by Pontus Gagge

The pimpl idiom helps you reduce build dependencies and times especially in large applications, and minimizes header exposure of the implementation details of your class to one compilation unit. The users of your class should not even need to be aware of the existence of a pimple (except as a cryptic pointer to which they are not privy!).

pimpl 习惯用法可帮助您减少构建依赖项和时间,尤其是在大型应用程序中,并最大限度地减少您的类的实现细节在一个编译单元中的头暴露。您班级的用户甚至不需要知道粉刺的存在(除非作为他们不知道的神秘指针!)。

Abstract classes (pure virtuals) is something of which your clients must be aware: if you try to use them to reduce coupling and circular references, you need to add some way of allowing them to create your objects (e.g. through factory methods or classes, dependency injection or other mechanisms).

抽象类(纯虚拟)是您的客户必须意识到的:如果您尝试使用它们来减少耦合和循环引用,您需要添加一些允许它们创建对象的方法(例如,通过工厂方法或类,依赖注入或其他机制)。

回答by Ilias Bartolini

I was searching an answer for the same question. After reading some articles and some practice I prefer using "Pure virtual class interfaces".

我正在寻找相同问题的答案。在阅读了一些文章和一些实践后,我更喜欢使用“纯虚拟类接口”

  1. They are more straight forward (this is a subjective opinion). Pimpl idiom makes me feel I'm writing code "for the compiler", not for the "next developer" that will read my code.
  2. Some testing frameworks have direct support for Mocking pure virtual classes
  3. It's true that you needa factory to be accessible from the outside. But if you want to leverage polymorphism: that's also "pro", not a "con". ...and a simple factory method does not really hurts so much
  1. 他们更直接(这是一个主观意见)。Pimpl 习惯用法让我觉得我是在“为编译器”编写代码,而不是为将阅读我的代码的“下一个开发人员”编写代码。
  2. 一些测试框架直接支持 Mocking 纯虚拟类
  3. 确实,您需要一个可以从外部访问的工厂。但是,如果您想利用多态性:这也是“优点”,而不是“缺点”。...而且一个简单的工厂方法并没有那么痛

The only drawback (I'm trying to investigate on this) is that pimpl idiom could be faster

唯一的缺点(我正在尝试对此进行调查)是 pimpl 成语可能更快

  1. when the proxy-calls are inlined, while inheriting necessarily need an extra access to the object VTABLE at runtime
  2. the memory footprint the pimpl public-proxy-class is smaller (you can do easily optimizations for faster swaps and other similar optimizations)
  1. 当代理调用被内联时,继承必然需要在运行时额外访问对象 VTABLE
  2. pimpl public-proxy-class 的内存占用更小(您可以轻松优化以实现更快的交换和其他类似优化)

回答by Masha Ananieva

I hate pimples! They do the class ugly and not readable. All methods are redirected to pimple. You never see in headers, what functionalities has the class, so you can not refactor it (e. g. simply change the visibility of a method). The class feels like "pregnant". I think using iterfaces is better and really enough to hide the implementation from the client. You can event let one class implement several interfaces to hold them thin. One should prefer interfaces! Note: You do not necessary need the factory class. Relevant is that the class clients communicate with it's instances via the appropriate interface. The hiding of private methods I find as a strange paranoia and do not see reason for this since we hav interfaces.

我讨厌痘痘!他们做的班级丑陋且不可读。所有方法都重定向到疙瘩。您永远不会在标题中看到类具有哪些功能,因此您无法重构它(例如,简单地更改方法的可见性)。这堂课感觉像是“怀孕”了。我认为使用 iterfaces 更好,而且确实足以向客户端隐藏实现。你可以让一个类实现几个接口来保持它们的精简。人们应该更喜欢接口!注意:您不需要工厂类。相关的是类客户端通过适当的接口与其实例进行通信。我发现隐藏私有方法是一种奇怪的偏执,因为我们有接口,所以没有看到这样做的原因。

回答by Masha Ananieva

There's a very real problem with shared libraries that the pimpl idiom circumvents neatly that pure virtuals can't: you cannot safely modify/remove data members of a class without forcing users of the class to recompile their code. That may be acceptable under some circumstances, but not e.g. for system libraries.

共享库有一个非常现实的问题,pimpl 习惯用法巧妙地规避了纯虚拟不能:您不能安全地修改/删除类的数据成员而不强制类的用户重新编译他们的代码。在某些情况下这可能是可以接受的,但对于系统库来说则不然。

To explain the problem in detail, consider the following code in your shared library/header:

要详细解释问题,请考虑共享库/标头中的以下代码:

// header
struct A
{
public:
  A();
  // more public interface, some of which uses the int below
private:
  int a;
};

// library 
A::A()
  : a(0)
{}

The compiler emits code in the shared library that calculates the address of the integer to be initialized to be a certain offset (probably zero in this case, because it's the only member) from the pointer to the A object it knows to be this.

编译器在共享库中发出代码,该代码计算要初始化的整数的地址,该地址是从指向它知道的 A 对象的指针的某个偏移量(在这种情况下可能为零,因为它是唯一的成员)this

On the user side of the code, a new Awill first allocate sizeof(A)bytes of memory, then hand a pointer to that memory to the A::A()constructor as this.

在代码的用户端, anew A将首先分配sizeof(A)内存字节,然后将指向该内存的指针A::A()作为传递给构造函数this

If in a later revision of your library you decide to drop the integer, make it larger, smaller, or add members, there'll be a mismatch between the amount of memory user's code allocates, and the offsets the constructor code expects. The likely result is a crash, if you're lucky - if you're less lucky, your software behaves oddly.

如果在您的库的后续修订版中决定删除整数,使其更大、更小或添加成员,则用户代码分配的内存量与构造函数代码期望的偏移量之间将不匹配。可能的结果是崩溃,如果你幸运的话——如果你不那么幸运,你的软件就会表现得很奇怪。

By pimpl'ing, you can safely add and remove data members to the inner class, as the memory allocation and constructor call happen in the shared library:

通过 pimpl'ing,您可以安全地向内部类添加和删除数据成员,因为内存分配和构造函数调用发生在共享库中:

// header
struct A
{
public:
  A();
  // more public interface, all of which delegates to the impl
private:
  void * impl;
};

// library 
A::A()
  : impl(new A_impl())
{}

All you need to do now is keep your public interface free of data members other than the pointer to the implementation object, and you're safe from this class of errors.

您现在需要做的就是让您的公共接口不包含指向实现对象的指针以外的数据成员,这样您就不会出现此类错误。

Edit:I should maybe add that the only reason I'm talking about the constructor here is that I didn't want to provide more code - the same argumentation applies to all functions that access data members.

编辑:我应该补充一点,我在这里谈论构造函数的唯一原因是我不想提供更多代码 - 相同的论证适用于访问数据成员的所有函数。

回答by Sam

We must not forget that inheritance is a stronger, closer coupling than delegation. I would also take into account all the issues raised in the answers given when deciding what design idioms to employ in solving a particular problem.

我们不能忘记继承是比委托更强大、更紧密的耦合。在决定使用哪些设计习惯来解决特定问题时,我还会考虑给出的答案中提出的所有问题。

回答by Superfly Jon

Although broadly covered in the other answers maybe I can be a bit more explicit about one benefit of pimpl over virtual base classes:

尽管在其他答案中广泛涵盖,也许我可以更明确地了解 pimpl 相对于虚拟基类的一个好处:

A pimpl approach is transparent from the user view point, meaning you can e.g. create objects of the class on the stack and use them directly in containers. If you try to hide the implementation using an abstract virtual base class, you will need to return a shared pointer to the base class from a factory, complicating it's use. Consider the following equivalent client code:

从用户的角度来看,pimpl 方法是透明的,这意味着您可以例如在堆栈上创建类的对象并直接在容器中使用它们。如果您尝试使用抽象虚拟基类隐藏实现,则需要从工厂返回指向基类的共享指针,这会使它的使用复杂化。考虑以下等效的客户端代码:

// Pimpl
Object pi_obj(10);
std::cout << pi_obj.SomeFun1();

std::vector<Object> objs;
objs.emplace_back(3);
objs.emplace_back(4);
objs.emplace_back(5);
for (auto& o : objs)
    std::cout << o.SomeFun1();

// Abstract Base Class
auto abc_obj = ObjectABC::CreateObject(20);
std::cout << abc_obj->SomeFun1();

std::vector<std::shared_ptr<ObjectABC>> objs2;
objs2.push_back(ObjectABC::CreateObject(13));
objs2.push_back(ObjectABC::CreateObject(14));
objs2.push_back(ObjectABC::CreateObject(15));
for (auto& o : objs2)
    std::cout << o->SomeFun1();

回答by Trantor

The most annoying problem about the pimpl idiom is it makes it extremely hard to maintain and analyse existing code. So using pimpl you pay with developer time and frustration only to "reduce build dependencies and times and minimize header exposure of the implementation details". Decide yourself, if it is really worth it.

pimpl 惯用语最烦人的问题是它使维护和分析现有代码变得极其困难。因此,使用 pimpl 只会为“减少构建依赖项和时间并最大程度地减少实现细节的标头暴露”而付出开发人员的时间和挫折。自己决定,是否真的值得。

Especially "build times" is a problem you can solve by better hardware or using tools like Incredibuild ( www.incredibuild.com, also already included in Visual Studio 2017 ), thus not affecting your software design. Software design should be generally independent of the way the software is built.

特别是“构建时间”是您可以通过更好的硬件或使用 Incredibuild(www.incredibuild.com,也已包含在 Visual Studio 2017 中)之类的工具来解决的问题,因此不会影响您的软件设计。软件设计通常应该独立于软件的构建方式。

回答by Robert S. Barnes

In my understanding these two things serve completely different purposes. The purpose of the pimple idiom is basically give you a handle to your implementation so you can do things like fast swaps for a sort.

在我的理解中,这两件事有完全不同的目的。pimple idiom 的目的基本上是为您提供实现的句柄,以便您可以执行诸如快速交换之类的操作。

The purpose of virtual classes is more along the line of allowing polymorphism, i.e. you have a unknown pointer to an object of a derived type and when you call function x you always get the right function for whatever class the base pointer actually points to.

虚拟类的目的更接近于允许多态性,即您有一个指向派生类型对象的未知指针,并且当您调用函数 x 时,您总是会为基指针实际指向的任何类获得正确的函数。

Apples and oranges really.

苹果和橘子真的。