1

If I am to inherit from a class, would I have to define all of its virtual and pure virtual functions?

For example, I have a derived class that is inheriting from QAbstractItemModel. QAbstractItemModel has the following pure virtual functions. If my derived class is not going to use the index() and parent() method, would I need to implement it?

//qabstractitemmodel.h
virtual QModelIndex index(int row, int column,
                              const QModelIndex &parent = QModelIndex()) const = 0;
virtual QModelIndex parent(const QModelIndex &child) const = 0;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const = 0;
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const = 0;
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const = 0;
Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
Jon
  • 8,205
  • 25
  • 87
  • 146
  • http://stackoverflow.com/questions/2089083/pure-virtual-function-with-implementation - yes, you will have to define the pure virtual functions in your derived class if that class will be instantiated. `QAbstractItemModel` doesn't provide definitions of `index` or `parent`, so you will need to supply your own. If it did supply implementations, you could call the base class's version from your subclass. – wkl Apr 09 '12 at 20:49
  • http://stackoverflow.com/questions/2089083/pure-virtual-function-with-implementation –  Apr 09 '12 at 20:52
  • possible duplicate of [C++ Virtual/Pure Virtual Explained](http://stackoverflow.com/questions/1306778/c-virtual-pure-virtual-explained) –  Apr 09 '12 at 20:53
  • http://stackoverflow.com/questions/2652198/difference-between-a-virtual-function-and-a-pure-virtual-function –  Apr 09 '12 at 20:53
  • http://stackoverflow.com/questions/2609299/use-cases-of-pure-virtual-functions-with-body –  Apr 09 '12 at 20:54

2 Answers2

3

You don't have to implement anything at all in your derived class, but that derived class will still be abstract if you leave any of the pure virtual member functions without an implementation (In other words, you won't be able to instantiate an object of that class).

Edit: Something else to consider - If your base class contains pure virtual functions which your derived classes do not want/need, maybe worth looking at an alternative design? Perhaps using multiple base classes which declare different parts of the interfaces.

If index() and parent() don't apply to all of the derived classes of QAbstractItemModel then I'd argue those functions possibly don't belong to QAbstractItemModel really

Ben Cottrell
  • 5,741
  • 1
  • 27
  • 34
  • 1
    I have seen it where a derived class was purposely left out of date with the base class because the specification had changed and new classes were created. While you could call the base class function on the out-of-date derived class, it had no effect. This all depends on your overall design. It's not bad just not ideal. –  Apr 09 '12 at 20:57
2

Yes. Declaring a method as pure virtual (' = 0') means that any concrete subclass (which can be instantiated) has to implement them.

Asaf
  • 4,317
  • 28
  • 48