dimanche 29 décembre 2019

Using swap vs assignment for out string parameter of a function

If we have a function which is supposed to populate the string value to its out parameter based on some calculation, then which one should we prefer to use and why?

void function(string& x)
{
    // In actual code, this value is calculated based on certain conditions
    string temp = "Stack Overflow";

    x.swap(temp);
}

Vs

void function(string& x)
{
    // In actual code, this value is calculated based on certain conditions
    string temp = "Stack Overflow";

    x = t;
}

Aucun commentaire:

Enregistrer un commentaire