dimanche 24 juillet 2016

Is this list garbage memory?

I have two class like this:

foo.h

class A //base class
{
public:
    A(QList<int> &timers, QList<int> &positions);
    int pIndex = 0;
    QList<int> timers;
    QList<int> positions;
};


class B : public A
{
public:
    B();
private:
    QList<int> t { 5, 8, 10, 20, 25 };
    QList<int> p { 10, 15, 20 };
};

foo.cpp

B::B()
    : A(t)
{
}

A::A(QList<int> &t, QList<int> &p)
{
    foreach (int v, t) {
       qDebug() << "v = " << v;
    }

    //this->timers = t;
    //this->positions = p;
}

but when I intitialize B class, it crashes saying about SGIFAULT.

auto b = new B();

What am I missing for get this error? at first I thought it was the QList that I pass passing that had become garbage memory at time but this code which follow same principle but use an int[] rather QList<int> does work fine:

class A
{
public:
    A(int p[3]) 
    {
        this->v = p;
    }

    void print()
    {
        for(int i = 0; i < 3; ++i)
        {
            printf("v = %d\n", v[i]);
        }
    }
private:
    int *v = NULL;
};

class B : public A
{
public:
    B() : A(values) 
    {
    }
private:
    int values[3] = {1, 2, 3};
};

int main() {
    A *o = new B();
    o->print();
}

Aucun commentaire:

Enregistrer un commentaire