如果我要继承一个类,我是否必须定义它的所有虚拟和纯虚函数?C++ - 继承虚函数实现
例如,我有一个继承自QAbstractItemModel
的派生类。 QAbstractItemModel
具有以下纯虚函数。如果我的派生类不打算使用index()
和parent()
方法,是否需要实现它?
//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;
http://stackoverflow.com/questions/2089083/pure-virtual-function-with-implementation - 是的,你将不得不在你的派生类中定义纯虚函数,如果该类将被实例化。 'QAbstractItemModel'不提供'index'或'parent'的定义,所以你需要提供你自己的。如果它提供了实现,你可以从你的子类调用基类的版本。 – birryree 2012-04-09 20:49:04
http://stackoverflow.com/questions/2089083/pure-virtual-function-with-implementation – 2012-04-09 20:52:49
[C++ Virtual/Pure Virtual Explained]的可能重复(http://stackoverflow.com/questions/1306778/c-virtual - 纯虚拟解释) – 2012-04-09 20:53:31