开发者

Qt - Is this pass by reference?

If we write the following in Qtas an argument to a function: QString &tableName

Does that m开发者_Python百科ean we are passing by reference?

Thanks.


Strictly speaking, that is a function parameter, not a function argument. The parameter is the variable declared inside the function's parameter list; the argument is the value passed to the function by the calling function. So parameter QString &tableName is passed by reference. But as a function argument, &tableName would mean "the address of tableName".

Updated: As requested, here is a code sample to clarify the distinction:

void f (double y) ;
f (99.0) ;

double y is a parameter declaration; it resembles a variable declaration. y is a function parameter.

99.0 is a function argument; it is an expression, that must be convertible to type double.


Yes, this is pass-by-reference in C++. You could also write QString const & tableName, if you don't want to have the very possibility of accidentally changing tableName.


Yes. Tip: make it const if you don't want it to change.


Note that, like most non-trivial Qt basic types, QString is a lightweight container object that implements "copy on write" semantics. So the only reason to pass one by reference is if your function wants to modify the caller's copy, and there is never any reason to pass one by const reference (unless you do not know much about Qt).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜