11/20/2013

d-pointer in Qt




My dear friends, How are you? Nice to see you after such a long time.  Nowadays I'm slightly moving to the Qt and hope to publish posts about it.   So let's move to the post.  Actually This is warm-up post ;)

D-pointer or opaque pointer  technique can be called as a design pattern.  According to the Wikipedia this technique same as Bridge pattern and sometimes  referred to as handle classes, the Pimpl idiom, Compiler firewall idiom or Cheshire Cat.

With d-pointer programmers gain some advantages.

1. Can be created simple interfaces.
2. Hide the implementation details of an interface from ordinary clients.
3. Provide binary code compatibility with different versions of a shared library.

You can implement d-pointer as this.

     

// MyClass.h

class MyClass
{
public:
    MyClass();
    ~MyClass();
    // implementation methods
private:
    class MyClassPrivate *d;
};

// MyClass.cpp
class MyClassPrivate
{
    int x;
};

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

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

References

 http://qt-project.org/wiki/Dpointer

No comments: