需求:将一系列操作步骤放在链表中按需调用。
我将每个操作步骤用一个类实现,将各类继承于一个基类,再将指向各子类的基类指针放在QVector/Vector,即可通过C++的多态实现调用各子类。
为此,我写了俩个范例测试代码.
一,各子类只单一继承同一基类
#include <iostream>
#include <vector>
using namespace std;
#include <QDebug>
class Base
{
public:
virtual ~Base() {}
virtual void printName();
};
void Base::printName()
{}
class Derived0 : public Base
{
public:
~Derived0();
void printName();
};
Derived0::~Derived0()
{}
void Derived0::printName()
{
qDebug() << "I am derived 0";
}
class Derived1 : public Base
{
public:
~Derived1();
void printName();
};
Derived1::~Derived1()
{}
void Derived1::printName()
{
qDebug() << "I am derived 1";
}
int main()
{
QVector<Base*> classVector;
Base* ptrDerived0 = new Derived0;
classVector.push_back(ptrDerived0);
Base* ptrDerived1 = new Derived1;
c