PDA

View Full Version : references or values?



Qiieha
24th April 2013, 10:17
Hi,
a trivial Question to you:

What's better style:
this


class Test{
private:
QString one;
QString two;

public:
Text(const QString& one,const QString& two);
}

//cpp

Test::Test(const QString& one,const QString& two) :
one(one),
two(two)
{
}

or that:


class Test{
private:
QString one;
QString two;

public:
Text(QString one, QString two);
}

//cpp

Test::Test(QString one,QString two) :
one(one),
two(two)
{
}


and what's are the advantages and disatvantages?

I prefer the first option, because it looks like more performance.. Am I right?

thanks ;)

anda_skoa
24th April 2013, 11:25
I would use the first one because it fits better into the overall coding style used by Qt.

Cheers,
_

RSX
25th April 2013, 11:37
The first one is actually more like general C++ coding style, second is used by Qt.

You can read about it here: http://qt-project.org/doc/qt-4.8/implicit-sharing.html

Lykurg
25th April 2013, 13:35
I would use both because they are not really comparable. Will say: I would use
const QString &oneand not
const QString oneif they should not be changeable, but I would use
QString oneand not
QString &oneif they should be changeable. (Because Implicit Sharing is cheap.)

Qiieha
26th April 2013, 13:25
thank u guys.
this is very good to know =)