Return type overloading in Qt



  • I have just noticed that in

    @
    template<class T>
    class QList;
    @

    there are some return type overloaded functions. For example:

    @
    T & last();
    const T & last() const;
    @

    In my situation I have an object:

    @
    QList<QSqlQuery> m_Queries;
    @

    I append a query and then I want to call it and exec it (if I did that beforehand I would be copying the result as well which might be quite big). Therefore I need the former T & last() function to be called to get the reference of the query and exec that in the list:

    @
    QString str("...");
    m_List.append(QSqlQuery query(str));
    m_List.last().exec();
    @

    How is the overload resolved in this situation?



  • Return types are not considered for overload resolution, however, constness is.

    @const T& last() const@

    is called when the QList is const (or accessed through a const reference or a pointer to const),

    @T& last()@

    is called otherwise.



  • Thanks for clearing that up for me!



  • Either way they are both references which will be small. No deep copy there.

    Also Qt is really smart. All (or at least most) of it's objects have smart pointers to their actual data.

    So:

    @
    QString str = "some huge string, just pretend :)";
    QString str2(str);
    @

    In that example the string str2 would not get a deep copy of the "huge" string, and instead would just have a pointer to it's data. The deep copy won't occur until you do something that changes str or str2.

    So in the case of your large query results, it shouldn't affect performance or memory much more than a small pointer.

    I haven't verified that QSqlQuery works that way, but like I said most Qt objects do, so I'm making an assumption here.



  • [quote author="ambershark" date="1404516823"]So in the case of your large query results, it shouldn't affect performance or memory much more than a small pointer.

    I haven't verified that QSqlQuery works that way, but like I said most Qt objects do, so I'm making an assumption here.[/quote]

    Hm, that is interesting. According to source code the copy of a QSqlQuery is indeed copying only the reference to the original query. I wonder though what causes the deep copy (if anything)?

    Functions (next(), seek(), first(), last()) that are used for navigating the result are non-const meaning that they "alter" the object. However since the result is different QSqlResult object and QSqlQuery object acts more like a wrapper for it, sipmly changing where it points to in the result object should not require copy of the result itself. And the result is what's big, not the wrapper. Afaik the result cannot be changed in any way through QSqlQuery once it is loaded afaik (except for deleting it).

    Seems like I needn't have to worry! But at least I have learnt something new (and yet again amazing) about Qt.



  • Yea these Qt guys are awesome. :)

    Anyway, I don't know exactly what causes a deep copy, but I think it's when the object's data changes.

    So in your case, if you modified the result set that would cause a copy, whereas if you just got a new result set it would just make a new one for you and no deep copy.

    Non-const functions rarely will copy the data which is usually the "md" pointer in the class. They may modify the class itself but only things that would modify the underlying data pointed to by their 'md' pointer would cause that copy.

    That being said I still try to be nice and pass things by reference or pointer rather than forcing any kind of new construction/copy of data. I like to know that at least if I am lazy as a programmer Qt has my back. ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.