The following example illustrates the [[Policies/Library_Code_Policy#Shared D-Pointers|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 following example illustrates the [[Policies/Library_Code_Policy#Shared D-Pointers|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. This example puts your d-pointer into {{qt|QScopedPointer}} which will handle proper deleting of the d-pointer once the main object is destroyed. Be sure to delete the d-pointer properly when not using {{qt|QScopedPointer}}, otherwise your d_ptr will leak.

== The header file, without Q_DECLARE_PRIVATE ==

== The header file, without Q_DECLARE_PRIVATE ==

Line 12:

Line 12:

int someInteger() const;

int someInteger() const;

protected:

protected:

−

const QScopePointer<KFooBasePrivate> d_ptr;

+

const QScopedPointer<KFooBasePrivate> d_ptr;

KFooBase(KFooBasePrivate &dd, QObject *parent);

KFooBase(KFooBasePrivate &dd, QObject *parent);

private:

private:

Line 46:

Line 46:

public:

public:

KFooBase(QObject *parent);

KFooBase(QObject *parent);

+

~KFooBase();

void setSomeInteger(int i);

void setSomeInteger(int i);

int someInteger() const;

int someInteger() const;

protected:

protected:

−

KFooBasePrivate * const d_ptr;

+

const QScopedPointer<KFooBasePrivate> d_ptr;

KFooBase(KFooBasePrivate &dd, QObject *parent);

KFooBase(KFooBasePrivate &dd, QObject *parent);

private:

private:

Line 90:

Line 91:

}

}

+

// The destructor must be in the .cpp file, even if empty

KFooBase::~KFooBase()

KFooBase::~KFooBase()

{

{

−

delete d_ptr;

}

}

Line 126:

Line 127:

: KFooBase(dd, parent)

: KFooBase(dd, parent)

{

{

−

}

−

−

KFooDerived::~KFooDerived()

−

{

−

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

}

}

Latest revision as of 08:14, 7 January 2013

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. This example puts your d-pointer into QScopedPointer which will handle proper deleting of the d-pointer once the main object is destroyed. Be sure to delete the d-pointer properly when not using QScopedPointer, otherwise your d_ptr will leak.

The .cpp file

/*** KFooBase ***/classKFooBasePrivate{public:virtual~KFooBasePrivate(){}intsomeInteger;};KFooBase::KFooBase(QObject*parent):QObject(parent),d_ptr(newKFooBasePrivate){}KFooBase::KFooBase(KFooBasePrivate&dd,QObject*parent):QObject(parent),d_ptr(&dd){}// The destructor must be in the .cpp file, even if emptyKFooBase::~KFooBase(){}voidKFooBase::setSomeInteger(inti){Q_D(KFooBase);d->someInteger=i;}intKFooBase::someInteger()const{// in const functions add 'const' to the class nameQ_D(constKFooBase);returnd->someInteger;}/*** KFooDerived ***/classKFooDerivedPrivate:publicKFooBasePrivate{public:intanotherInteger;};KFooDerived::KFooDerived(QObject*parent):KFooBase(*newKFooDerivedPrivate,parent){}KFooDerived::KFooDerived(KFooDerivedPrivate&dd,QObject*parent):KFooBase(dd,parent){}voidKFooDerived::setAnotherInteger(inti){Q_D(KFooDerived);d->anotherInteger=i;}intKFooDerived::anotherInteger()const{// in const functions add 'const' to the class nameQ_D(constKFooDerived);returnd->anotherInteger;}intKFooDerived::sumOfIntegers()const{// in const functions add 'const' to the class nameQ_D(constKFooDerived);returnd->someInteger+d->anotherInteger;}