简体   繁体   中英

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

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, ie 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!

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM