Search code examples
c++qtqmlfunction-definition

Assign function as default parameter using & operator


I was studying an example of setting information inside of a QML table in Qt and I don't understand what is being passed as default parameter:

int rowCount(const QModelIndex & = QModelIndex()) const override
{
    return 200;
}

The source of the example can be found here.

My guess is that const QModelIndex & = QModelIndex() means that the default parameter is a reference to the function QModelIndex(), which is an inline function. Is this correct? Can someone explain to me what is happening here, please?


Solution

  • As stated in comments:

    int rowCount(const QModelIndex & = QModelIndex()) const override
    

    defines the overridden member function rowCount, its one unnamed argument has a default value of QModelIndex(). For any type T, T::T() defines its default constructor, so QModelIndex() is a default constructed QModelIndex object. This pattern of defining an unnamed member function argument is common practise when deriving from an abstract interface where the overriding implementation has no use for the argument.