的动机
我正在学习Model-View (paragraph Using a model)在Qt中使用模型的教程。
该示例显示以下代码:
//Header file
class StringListModel : public QAbstractListModel
//main.cpp
QAbstractItemModel *model = new StringListModel(numbers);
用下面的解释。
我这样解释他们的答案:
您可以在
model
上开发项目,如果您需要更改基础数据结构,则可以这样操作://Header file
class NewStringListModel : public QAbstractListModel
//main.cpp
//QAbstractItemModel *model = new StringListModel(numbers); Stoped using this, old
QAbstractItemModel *model = new NewStringListModel(numbers); //complete model change with just a single line
广义
就像这样:
pointerToParent->childMethodNotExistingInParent()
最佳答案
dynamic_cast<>()
尝试将父指针转换为所需的任何子类,然后以这种方式访问新功能。但是,这不是理想的方法。最好重构父/子类,并使用virtual
函数来避免这种情况。 关于c++ - 在指向父对象的指针上初始化子对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19682402/