使用 d 指针有哪些优点和缺点?

发布于 2024-10-17 07:44:01 字数 442 浏览 7 评论 0原文

d 指针在 Qt 中被大量使用,它们是 pimpl idiom 的实现。我知道 pimpl 习语的优点和缺点。但我错过了 d 指针实现的优点。 此处这里是 d 指针的示例。 直接用这个不是更方便吗?

class MyClassPrivate;
class MyClass {
  // interface methods
private:
  MyClassPrivate *pimpl_;
};

d-pointers are heavily used in Qt, they are an implementation of pimpl idiom. I know advantages and disadvantages of pimpl idiom. But I have missed the advantages of d-pointers implementation. Here and here are the samples of d-pointers.
Isn't it easier to just use this?

class MyClassPrivate;
class MyClass {
  // interface methods
private:
  MyClassPrivate *pimpl_;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苦笑流年记忆 2024-10-24 07:44:01

d 指针是 pimpl 模式的众多实现之一。它也是早期的实现之一:“‘d-pointer’这个名称源自 Trolltech 的 Arnt Gulbrandsen,他首先将该技术引入 Qt,使其成为首批 C++ GUI 库之一,即使在更大的库之间也能保持二进制兼容性。 " 来源

使用宏的一个优点是可以选择更改一些实现细节模式实现在编译时的中心位置。例如,您可以设计宏,让您可以选择切换到快速 pimpl 实现稍后无需更改大量代码(希望如果您使用 pimpl :-),您将不需要它)。假设您在宏设计/实现中没有犯任何错误...

但是,我个人建议您避免在 pimpl 实现中使用宏,因为它们对于源代码树的任何新手来说都是神秘的。宏创建的神奇方言通常容易出错,而且不如原始源代码有意义。它们还存在与 C 预处理器相关的所有问题;它不知道底层语言。

我个人喜欢使用所谓的 d 参考。您可以使用引用而不是指针,并且不必进行 D 引用。 8-) 它看起来像这样:

// MyClass.h

class MyClass
{
public:
    MyClass();
    ~MyClass();

    // implementation methods

private:
    class MyClassPrivate& d;
};

// MyClass.cpp

struct MyClassPrivate
{
    int x;
};

MyClass::MyClass()
: d(*new MyClassPrivate)
{

}

MyClass::~MyClass()
{
    delete &d;
}

// In methods use d.x

d-pointers are one implementation, among many, of the pimpl pattern. It is also one of the early implementations: "The name 'd-pointer' stems from Trolltech's Arnt Gulbrandsen, who first introduced the technique into Qt, making it one of the first C++ GUI libraries to maintain binary compatibility even between bigger release." Source

One advantage of using macros is the option of changing some implementation details of the pattern implementation in a central place at compile time. You could for example design your macros to leave you the option of switching to the fast pimpl implementation at a later time without changing tons of code (hopefully you won't need this if you are using pimpl :-)). Provided that you made no mistakes in your macro design/implementation...

However, I would personally recommend avoiding macros for your pimpl implementation as they are cryptic for any newcomer to your source tree. Macros create magical dialects that are often error-prone and not as meaningful as the original source code. They also come with all the problems associated with the C Pre Processor; it's unaware of the underlying language.

Personally I like to use what I call a d-reference. Instead of a pointer, you use a reference and you don't have to d-reference. 8-) It looks something like this:

// MyClass.h

class MyClass
{
public:
    MyClass();
    ~MyClass();

    // implementation methods

private:
    class MyClassPrivate& d;
};

// MyClass.cpp

struct MyClassPrivate
{
    int x;
};

MyClass::MyClass()
: d(*new MyClassPrivate)
{

}

MyClass::~MyClass()
{
    delete &d;
}

// In methods use d.x
む无字情书 2024-10-24 07:44:01

d 指针模式的宏集提供了某种排序便利性和一致性。例如,Q_DECLARE_PRIVATE 确保 Foo 的 pimpl 私有类被命名为 FooPrivate,FooPrivate 与 Foo 成为朋友,并创建一个名为 d_func() 的漂亮内联函数(const 和非常量版本)。后者用于 Q_D 宏,它基本上创建一个指向私有类实例的作用域/局部变量 d 。

简而言之,您不需要使用 Q_DECLARE_PRIVATE 和其他宏,但这样做会使代码更短、更清晰且一致。

The set of macros for d-pointer pattern provides some sort convenience and consistencies. For example, Q_DECLARE_PRIVATE ensures that the pimpl private class for Foo is named as FooPrivate, that FooPrivate befriends Foo, and creates an nice inline function (both const and nonconst versions) called d_func(). The latter is used in Q_D macro, which basically creates a scoped/local variable d which points to the private class instance.

In short, you need not use Q_DECLARE_PRIVATE and other macros, but doing so will make the code shorter, cleaner, and consistent.

反目相谮 2024-10-24 07:44:01

我认为 d 指针实际上只是一些用于实现 pimpl 习惯用法的方便宏。只需看一下所有这些宏的定义:Q_DQ_QQ_DECLARE_PRIVATE 等。它们只是部分的快捷方式。 >粉刺。例如,大多数时候您希望将对原始类的引用保留在私有类中。为此有 Q_QQ_DECLARE_PUBLIC。毕竟,使用宏迫使所有团队都统一实现pimpl。否则,有些人会调用私有指针 d,其他人会调用 pimpl_,想象一下这个混乱的情况。

I think that d-pointers are really just a few handy macros for implementing the pimpl idiom. Just take a look at the definition of all these macros: Q_D, Q_Q, Q_DECLARE_PRIVATE, etc. They are just shortcuts for parts of pimpl. For example most of the time you want to keep the reference to the original class in the private class. There are Q_Q and Q_DECLARE_PUBLIC for this. After all, using the macros forces all the team to have a uniform implementation of pimpl. Otherwise some people would call the private pointer d, others pimpl_, imagine this mess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文