Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the pros and cons of using d-pointers?

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_;
};
like image 212
Dmitriy Avatar asked Feb 13 '11 11:02

Dmitriy


2 Answers

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
like image 89
ppl Avatar answered Sep 19 '22 18:09

ppl


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.

like image 45
Ariya Hidayat Avatar answered Sep 23 '22 18:09

Ariya Hidayat