vendredi 29 janvier 2016

Is this copying values twice?

Let's assume I have a class like this:

class Foo
{
  QString a;
  void setA(QString val);
}

and implement setA() like this:

void Foo::setA(QString val)
{
  this->a = val;
}

and use it like:

Foo f;
QString v = "foo";
f.setA(v);

Am I copying the v struct twice on stack? one for pass in the argument and other in the assignment within the function, is this right? by rather using void setA(QString &val); will avoid copying the object twice because in the first one I'm just copying a reference (a pointer) and not the entire object so the only copy of the object is in the assignment:

  this->a = val;

Aucun commentaire:

Enregistrer un commentaire