Doubt about inheritance and call to functions in base class

-1

The doubt has come for Qt, but it is C ++.

If I have a class structure like this: (I only define eventFilter )

class DelegadoBase : public QStyledItemDelegate
{
    Q_OBJECT
public:

    explicit DelegadoBase(QObject* parent=nullptr);
    bool eventFilter(QObject *obj, QEvent* event);
};

And this derived class:

class DelegadoEditorNumeros : public DelegadoBase
{
public:
    explicit DelegadoEditorNumeros(QObject* parent=nullptr);
    QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem&option, const QModelIndex&index) const;
    void setEditorData(QWidget * editor, const QModelIndex&index)const;
    void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex&index) const;
    void paint( QPainter *painter,const QStyleOptionViewItem &option, const QModelIndex &index ) const;
    QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const;
    QString displayText(const QVariant & value, const QLocale & locale) const;

private:
    QRegExp* rx;
};

In the paint() method of the derived class, when I call the base class method, it is assumed that I should call the method paint() of DelegadoBase , and that it calls the method paint() of QStyledItemDelegate , but not being defined in DelegadoBase I get the doubt of how it would be right.

It would be something like this:

void DelegadoEditorNumeros::paint( QPainter *painter,const QStyleOptionViewItem &option, const QModelIndex &index ) const
{ 
if (condicion)
    {              
        haz las cosas asi
    else
    {
        DelegadoBase::paint(painter, option, index);//así
        //QStyledItemDelegate::paint(painter, option, index);o así
    }    
    
asked by user3733164 13.11.2017 в 18:44
source

1 answer

2

At the moment when, in a class, a method is declared as virtual , a table of function pointers associated with the class is created. In this table, redeclared calls are stored in each derived class.

If a function is not redeclared in the parent class but in the grandfather class, absolutely nothing happens, the compiler will consult the aforementioned table and call the first Function available:

  • If the function is redeclared in the parent , it will be called.
  • If it is not declared in the father but in the grandfather it will be called the grandfather's
  • If it is not declared in the grandfather but in the great-grandfather ...

Likewise, you can skip the implementation of the parent by calling Abuelo::funcion() directly.

A practical example:

struct A
{
  virtual void func1()
  { std::cout << "A::func1()\n"; }

  virtual void func2()
  { std::cout << "A::func2()\n"; }

  virtual void func3()
  { std::cout << "A::func3()\n"; }
};

struct B : A
{
  void func1() override
  { std::cout << "B::func1()\n"; }

  void func3() override
  { std::cout << "B::func3()\n"; }
};

struct C : B
{
  void func1() override
  {
    std::cout << "C::func1()\n";
    B::func1(); // Llama a la funcion del padre -> existe
  }

  void func2() override
  {
    std::cout << "C::func2()\n";
    B::func2(); // Llama a la funcion del padre -> no existe
  }

  void func3() override
  {
    std::cout << "C::func3()\n";
    A::func3(); // Llama directamente a la funcion del abuelo
  }
};

You can see it working here

    
answered by 13.11.2017 / 19:24
source