0

I'm trying to us Qt for WebAssembly. I'm using QVector, but I want to add some convenient finder methods. So I've tried subclassing like the code below. The compiler complains that vec.begin() and vec.end() do not exist. If I change vec from a Foo_Vector to a QVector (no subclassing), it works.

Other methods from QVector also become undefined when using the subclass version -- such as size.

I tried defining a method on Foo_Vector:

size_t size() const { return QVector<Foo>::size(); }

That produces an error about non-static methods. So I tried to do this:

    QVector<Foo> * ptr = static_cast<QVector<Foo>>(this);

And that produces an error that there's no conversion from a Foo_Vector * to a QVector *.

Does anyone have an explanation? Do I have to do this through composition instead and implement passthrough methods for anything I want to do?

#include <QCoreApplication>
#include <QVector>

class Foo {
public:
    int a;
    int b;
};

class Foo_Vector: public QVector<Foo> {
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Foo_Vector vec;

    for (auto it = vec.begin(); it != vec.end(); ++it) {
    }

    return a.exec();
}
Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • QVector does not have a virtual destructor, so it will be dangerous to subclass. See answers [here](https://stackoverflow.com/questions/22998663/how-to-subclass-qvector), and [here](https://stackoverflow.com/questions/25995642/subclassing-qvectort) for more info and examples. – JarMan Sep 23 '20 at 16:24
  • 1
    Subclassing in that way it's a terrible idea. Look at the std implementation of sort etc.. write functions accepting the vector you need or a generic iterator. – Moia Sep 23 '20 at 16:25
  • 1
    I think I maybe haven't purged all my old Java habits. – Joseph Larson Sep 23 '20 at 16:36

0 Answers0