Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
  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 = QColor();    
}
  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?
like image 720
MarkZltr Avatar asked Dec 12 '25 11:12

MarkZltr


1 Answers

You can add move semantics of course, but in your case there is no need in this at all. quint32, quint16 are moved by copying. QColor is wrapper around union and has no move constructor (and doesn't need one) and will also be moved by copying. QString is reference counted type in QT. It has move constructor in recent versions of library, but the difference in speed will be minimal (difference between swapping pointer and incrementing reference counter).

like image 60
Elohim Meth Avatar answered Dec 15 '25 06:12

Elohim Meth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!