Policies/Library Code Policy/Shared D-Pointer Example: Difference between revisions

From KDE TechBase
No edit summary
(for const functions use d_func() instead of Q_D)
Line 8: Line 8:
public:
public:
     KFooBase(QObject *parent);
     KFooBase(QObject *parent);
    void setSomeInteger(int i);
     int someInteger() const;
     int someInteger() const;
protected:
protected:
Line 42: Line 43:
public:
public:
     KFooBase(QObject *parent);
     KFooBase(QObject *parent);
    void setSomeInteger(int i);
     int someInteger() const;
     int someInteger() const;
protected:
protected:
Line 55: Line 57:
public:
public:
     KFooDerived(QObject *parent);
     KFooDerived(QObject *parent);
    void setSomeInteger(int i);
     int sumOfIntegers() const;
     int sumOfIntegers() const;
protected:
protected:
Line 86: Line 89:
{
{
     delete d_ptr;
     delete d_ptr;
}
void KFooBase::setSomeInteger(int i)
{
    Q_D(KFooBase);
    d->someInteger = i;
}
}


int KFooBase::someInteger() const
int KFooBase::someInteger() const
{
{
     Q_D(const KFooBase);
     // because of 'const' use d_func here instead of Q_D
     return d->someInteger;
     return d_func()->someInteger;
}
}


Line 119: Line 128:
int KFooDerived::sumOfIntegers() const
int KFooDerived::sumOfIntegers() const
{
{
     Q_D(const KFooDerived);
     return d_func()->someInteger + d_func()->anotherInteger;
    return d->someInteger + d->anotherInteger;
}
}
</code>
</code>

Revision as of 12:44, 12 September 2007

The following example illustrates the shared d-pointer concept. The class presented here derives from QObject to illustrate parameter-passing in the constructor; it is not necessary to be like that in your code.

The header file, without Q_DECLARE_PRIVATE

class KFooBasePrivate; class KFooBase : public QObject { public:

   KFooBase(QObject *parent);
   void setSomeInteger(int i);
   int someInteger() const;

protected:

   KFooBasePrivate * const d_ptr;
   KFooBase(KFooBasePrivate &dd, QObject *parent);

private:

   friend class KFooBasePrivate;
   inline KFooBasePrivate *d_func() { return d_ptr; }
   inline const KFooBasePrivate *d_func() const { return d_ptr; }

};

class KFooDerivedPrivate; class KFooDerived : public KFooBase { public:

   KFooDerived(QObject *parent);
   int sumOfIntegers() const;

protected:

   KFooDerived(KFooDerivedPrivate &dd, QObject *parent);

private:

   friend class KFooDerivedPrivate;
   inline KFooDerivedPrivate *d_func()
   { return reinterpret_cast<KFooDerivedPrivate *>(d_ptr); }
   inline const KFooDerivedPrivate *d_func() const
   { return reinterpret_cast<KFooDerivedPrivate *>(d_ptr); }

};

The header file, with Q_DECLARE_PRIVATE

class KFooBasePrivate; class KFooBase : public QObject { public:

   KFooBase(QObject *parent);
   void setSomeInteger(int i);
   int someInteger() const;

protected:

   KFooBasePrivate * const d_ptr;
   KFooBase(KFooBasePrivate &dd, QObject *parent);

private:

   Q_DECLARE_PRIVATE(KFooBase)

};

class KFooDerivedPrivate; class KFooDerived : public KFooBase { public:

   KFooDerived(QObject *parent);
   void setSomeInteger(int i);
   int sumOfIntegers() const;

protected:

   KFooDerived(KFooDerivedPrivate &dd, QObject *parent);

private:

   Q_DECLARE_PRIVATE(KFooDerived)

};

The .cpp file

/*** KFooBase ***/ class KFooBasePrivate { public:

   virtual ~KFooBasePrivate() { }
   int someInteger;

};

KFooBase::KFooBase(QObject *parent)

   : QObject(parent), d_ptr(new KFooBasePrivate)

{ }

KFooBase::KFooBase(KFooBasePrivate &dd, QObject *parent)

   : QObject(parent), d_ptr(&dd)

{ }

KFooBase::~KFooBase() {

   delete d_ptr;

}

void KFooBase::setSomeInteger(int i) {

   Q_D(KFooBase);
   d->someInteger = i;

}

int KFooBase::someInteger() const {

   // because of 'const' use d_func here instead of Q_D
   return d_func()->someInteger;

}

/*** KFooDerived ***/

class KFooDerivedPrivate: public KFooBasePrivate { public:

   int anotherInteger;

};

KFooDerived::KFooDerived(QObject *parent)

   : KFooBase(*new KFooDerivedPrivate, parent)

{ }

KFooDerived::KFooDerived(KFooDerivedPrivate &dd, QObject *parent)

   : KFooBase(dd, parent)

{ }

KFooDerived::~KFooDerived() {

   /* no need to delete the d-pointer! */

}

int KFooDerived::sumOfIntegers() const {

   return d_func()->someInteger + d_func()->anotherInteger;

}