mardi 4 août 2015

Move semantics in Qt without pointers?

I have a Qt project, there I have an Object, which is going to be copied a lot of time. Therefor I would like to add move semantics.

#ifndef OBJECTTOCOPY_H
#define OBJECTTOCOPY_H

#include <QColor>
#include <QString>
#include <QDataStream>
namespace level_1     {
namespace level_2 {
class ObjectToCopy {
public:

  explicit ObjectToCopy(const QString& _name = "", const QColor& colorBody = QColor() );

  // MOVE
  explicit ObjectToCopy(ObjectToCopy && other);  

  static quint32 _valueInt32;
  static quint16 _valueInt16;
  QString _name;
  QColor  _colorBody;

private:
};
}
}
#endif // OBJECTTOCOPY_H

How do I steal the pointers of the member variables, since they are no pointers?

ObjectToCopy::ObjectToCopy (ObjectToCopy&& other)
    : _valueInt32( other._valueInt32  )
    , _valueInt16( other._valueInt16 )
    , _name( other._name )
    , _colorBody( other._colorBody )
{
    other._valueInt32 = 0;
    other._valueInt16 = 0;
    other.name.clear();
    other._colorBody = nullptr;    
}

  1. Does that make sense for non-pointers?
  2. Is it ok to reset QString 's like string.clear(); to mark that for the garbage collector?
  3. How could I reset a QColor object?

Aucun commentaire:

Enregistrer un commentaire