[bt-devel] Refactoring displaywindows classes; a future feature

Gary Holmlund gary.holmlund at gmail.com
Thu Nov 19 13:00:12 MST 2009


On 11/19/2009 02:54 PM, Eeli Kaikkonen wrote:
> On Thu, 19 Nov 2009, Martin Gruner wrote:
>
>    
>> Eeli,
>>
>>      
>>> Also I want to
>>> get rid of passing module pointers and use QStrings and QStringLists
>>> which lessen the probability of crashes when modifying something. (As a
>>> side note QString and QStringList copies shouldn't be afraid of, its
>>> very cheap to copy them. See the Qt documentation for more.)
>>>        
>> I don't agree. It is never as cheap as copying a pointer. And if you just
>> switch from pointers to QStringLists that doesn't change the problem of
>> ownersphip. If you need to have a reference to a CSwordModuleInfo, a pointer
>> is a good thing to do that. Maybe we should centralize the places where these
>> pointers are held, but not copy around lists of module names.
>>      
> I don't know if we mean same things. First, I'm not changing the
> majority of messagin in text windows. I'm trying to find out how it
> really works and meantime I noticed that it's better to change only the
> module selector buttonbar/buttons. They don't actually need moduleinfo
> in most places. And a window already has a list of modules, but as
> string list! The pointer list is generated on-the-fly in a getter
> method (I only put similar code in the backend). So, copying string
> lists is actually cheaper.
>
> In any case, the Qt documentation clearly says that "Implicitly
> shared classes are both safe and efficient when passed as arguments" and
> "you can pass instances of these classes as arguments to functions by
> value without concern for the copying overhead". Actually, if I have
> understood correctly, copying a QString or QStringList is about as cheap
> as copying a reference counted pointer! And even if it wasn't, the
> overhead is minimal in functions which are called only once in a row.
> Some loops may of course be a different thing.
>
>    
I agree with you about the efficiency of Qt classes.
See: http://qt.nokia.com/doc/4.5/shared.html

Another technique is to pass variables as a const reference (const 
QString& str). The code generated is just the passing of a pointer. It 
works for any class, not just Qt, and you have a guarantee that the 
called function cannot change anything about the variable. At my work we 
always use const reference for a variable we don't expect the called 
function to change and we only use a pointer if we are expecting a 
returned value in the variable.

Gary



More information about the bt-devel mailing list