Pimpl copying

Clarence Dang dang at kde.org
Fri Jul 14 13:12:08 BST 2006


On Friday 14 July 2006 17:13, Peter Kümmel wrote:
> Clarence Dang wrote:
> > On Friday 14 July 2006 07:05, Peter Kümmel wrote:
> >> +KTempDir& KTempDir::operator=(const KTempDir& rhs)
> >> +{
> >
> > BTW, in general (but not this case), you need:
> >
> > if (this == &rhs)
> > 	return *this;
>
> But I don't understand why not in this case?

class KTempDir::Private
{
public:
   int _error;
   QString _tmpName;
   bool _exists;
   bool _autoDelete;
};

In this case, if this == &rhs, you would be assigning int's, QString's and 
bool's to themselves - fairly harmless.  But just imagine code like this:

kpPenBrush &kpPenBrush::operator= (const kpPenBrush &rhs) const
{
    d->ref--;
    if (d->ref == 0)
        delete d;  <-- danger

    d = rhs.d;  <-- if this == &rhs, rhs.d may have been deleted!
    d->ref++;
}





More information about the kde-core-devel mailing list