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

1

2

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!

Nasia answered 18/8, 2014 at 16:32 Comment(0)
V
4

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;
}
Vermicide answered 18/8, 2014 at 16:52 Comment(2)
std::shared_ptr or std::unique_ptr may be better than QSharedPtr.Mullens
Thanks guys, although I had it figured out by the time the first answer (now deleted) was posted! But your clear and complete answer will certainly be very helpful to other people ;) CheersNasia

© 2022 - 2024 — McMap. All rights reserved.