Skip to content

Basic D Pointer template (Pimpl idiom implementation)

Heikki Johannes Hildén edited this page May 1, 2016 · 3 revisions
// -----------------------------------------------------------------------------
// tofu.h

#ifndef TOFU_H
#define TOFU_H

#include <QWidget>
#include <QScopedPointer>

class TofuPrivate;

class Tofu : public QWidget
{
    Q_OBJECT

public:
    explicit Tofu(QWidget *parent = 0);
    ~Tofu();

protected:
    const QScopedPointer<TofuPrivate> d_ptr;

private:
    Q_DISABLE_COPY(Tofu)
    Q_DECLARE_PRIVATE(Tofu)
};

#endif // TOFU_H

// -----------------------------------------------------------------------------
// tofu_p.h

#ifndef TOFU_P_H
#define TOFU_P_H

#include "tofu.h"

class TofuPrivate
{
    Q_DISABLE_COPY(TofuPrivate)
    Q_DECLARE_PUBLIC(Tofu)

public:
    TofuPrivate(Tofu *parent)
        : q_ptr(parent)
    {
    }

    Tofu *const q_ptr;
};

#endif // TOFU_P_H


// -----------------------------------------------------------------------------
// tofu.cpp

#include "tofu.h"
#include "tofu_p.h"

Tofu::Tofu(QWidget *parent)
    : QWidget(parent),
      d_ptr(new TofuPrivate(this))
{
}

Tofu::~Tofu()
{
}