Originally Posted by
stampede
IMHO, passing a QList, QString ect. by value is bad.
I know about implicit data sharing in QT, but this can introduce bad programming habits in general (especially for begginers).
Passing by reference is a much worse habbit and is ambiguous.
Take a look at this snippet, is "x" passed by value or by reference?
int x = 7;
doSomething(x);
qDebug() << x;
int x = 7;
doSomething(x);
qDebug() << x;
To copy to clipboard, switch view to plain text mode
If you expect doSomething() to change "x", it is cleaner to pass it by pointer
int x = 7;
doSomething(&x);
qDebug() << x;
int x = 7;
doSomething(&x);
qDebug() << x;
To copy to clipboard, switch view to plain text mode
Some C++ purists will complain but the fact is it is a construct that doesn't force you to look at the declaration of doSomething().
If you don't want such constructs, you can always pass "x" by value or by a const reference and use the return value:
SomeType x = 7;
x = doSomething(x);
SomeType x = 7;
x = doSomething(x);
To copy to clipboard, switch view to plain text mode
Compare the three situations and decide yourself which ones are clear and which are not. Especially the last syntax is where implicit sharing helps.
Bookmarks