[mythtv] API suggestions
Joseph A. Caputo
jcaputo1 at comcast.net
Fri Feb 6 10:27:42 EST 2004
On Friday 06 February 2004 06:26, Henrik (Mauritz) Johnson wrote:
> Hi, a few questions and suggestions regarding the API. These are not
> in any particular order and I'll probably find more stuff as I keep
> working with it.
>
> 1. The use of QString and const is totally messed up. For instance
> why use the "const QString" datatype... ever? The correct usage
> should be "const QString &" which will save one extra copy. I know
> that QStrings are shared and therefor quickly copied, but that
> doesn't make it right. Someone should go through the API and clean
> that up. (I volunteer if someone give me CVS access).
Just to clarify, the proper usage of QString when calling functions is
as follows:
-As a value-based argument to a function (read-only): "const QString &"
(you could also pass "QString" or "const QString" by value without
incurring too much overhead, since the class is implicitly shared, but
if you don't need write access to the argument, then pass-by-reference
is preferred)
-As a reference-based argument to a function (caller expects argument to
be modified inside the call): "QString &"
(I imagine this is not encouraged)
-As the return value of a function: "QString"
Most definitely *never* return a "QString &" or "const QString &" from a
function unless you are very sure of what you are doing!
The above rules apply for any of Qt's implicitly shared (or
"copy-on-write") classes.
If you take a look at the Qt API reference, and within the Qt code base,
you'll see that Trolltech follows these rules.
-JAC
More information about the mythtv-dev
mailing list