Making a QList of an abstract class objects in C++/QT?

我与影子孤独终老i 提交于 2019-12-11 02:53:56

问题


although I've been helped countless times by other questions/answers here, this is my first question here, so don't be too harsh on me! :)

I've been learning QT/C++ and let's assume I have something like this:

class AbstractMasterClass{
public:
    virtual void foo(void) = 0;   //Pure virtual method
}

This class will have plenty subclasses, each one of them implementing their own foo() method. And the question is: How can I create a QList which I'll populate with AbstractMasterClass's subclasses?

The objective is to be able to iterate through the list calling the foo() method for every element, i.e. use the master class the same way I would do to a Java interface. The few tries I ended up getting several compile time errors saying that we can't allocate an object of an abstract class (obviously) while creating the QList.

So how can I do it or is there a better way to make a java like interface in C++/QT?

Thank you all in advance for your time answering or pointing me in the right direction!


回答1:


This is a general C++ question more than a Qt issue. You would want to use polymorphism in this case. Create a pointer of type AbstractMasterClass and make it point to one of your derived classes, then you store the pointers in your list. I used QSharedPtr in the example below to avoid you needing to do any manual deletion of memory.

class AbstractMasterClass {
public:
    virtual ~AbstractMasterClass(){};  // virtual destructor so derived classes can clean up
    virtual void foo() = 0;
};

class DerivedA : public AbstractMasterClass {
public:
    void foo() { cout << "A\n"; }
};

class DerivedB : public AbstractMasterClass {
public:
    void foo() { cout << "B\n"; }
};

int main() {
    QList<QSharedPtr<AbstractMasterClass>> myList;

    QSharedPtr<AbstractMasterClass> a(new DerivedA());
    QSharedPtr<AbstractMasterClass> b(new DerivedB());

    myList.push_back(a);
    myList.push_back(b);

    for (auto &it : myList) {
        it->foo();
    }

    return 0;
}


来源:https://stackoverflow.com/questions/25367972/making-a-qlist-of-an-abstract-class-objects-in-c-qt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!