We all know the usual recursion, for example:

функция имя(){ условие_завершения; имя(); } 

It is clear that this function will exist in memory in several instances until the completion of the entire call chain.

Here I met this code:

 class SimpleDelegate : public QStyledItemDelegate { public: void paint(QPainter* pPainter, const QStyleOptionViewItem& option, const QModelIndex& index ) const ...... условие_выхода ...... { QStyledItemDelegate::paint(pPainter, option, index); // рекурсия ? } }; 

It seems to be recursion too, but how does it work? Either a copy of the base object is created, or just a method is duplicated for the current object, or .., in general, is not clear.

    2 answers 2

    There is no recursion class member function definition.

     class SimpleDelegate : public QStyledItemDelegate { public: void paint(QPainter* pPainter, const QStyleOptionViewItem& option, const QModelIndex& index ) const ...... условие_выхода ...... { QStyledItemDelegate::paint(pPainter, option, index); // рекурсия ? } }; 

    There are two different functions. One function is SimpleDelegate::paint , the other function is QStyledItemDelegate::paint .

    The first function in the definition domain of the derived class hides the declaration of the second function with the same name in the base class. Therefore, if you want to call the base class function of the same name from the function of the derived class, you need to specify its qualified name, which is done in the given example.

    View this demo.

     #include <iostream> #include <string> int main() { struct A { void f(const char *) const { std::cout << "Hello"; } void f(char) const { std::cout << ' '; } }; struct B : A { void f(const std::string &s ) const { A::f(nullptr); A::f(' '); std::cout << s << std::endl; } }; B().f(std::string("perfect")); return 0; } 

    Its output to the console

     Hello perfect 

    Here the function declared in the derived class B hides the functions of the same name declared in the base class A Therefore, to refer to these functions of the base class in the domain of the derived class, you should use the qualified names of the functions of the base class.

    Another alternative approach, provided that there is no ambiguity in overloading functions, is to use using declarations in a derived class. For example,

     struct B : A { using A::f; void f(const std::string &s ) const { f(nullptr); f(' '); std::cout << s << std::endl; } }; 

    However, if the signatures of the functions of the same name in the base and derived classes are the same, then, in order to eliminate ambiguity, it is necessary to explicitly indicate the qualified names of functions for the base class.

    • one
      Thank you, while driving home, I realized this myself. ) Just hurry before leaving home. But I didn’t know about using using for referring to the parent, actually, thanks for that, it’s nice to see such detailed answers) - perfect
    • @perfect Not at all. Ask more. :) - Vlad from Moscow

    QStyledItemDelegate::paint(pPainter, option, index); will call the paint method on the QStyledItemDelegate class, which is basic relative to SimpleDelegate . This is not a recursive call.

    It is clear that this function will exist in memory in several instances.

    functions have no instances.