samedi 19 août 2017

Moving vector pointer to derived class of vector in C++?

I want to move the pointer of a vector to my the vector of the A object (this). I want to do this because I use my help vector (for mergesort) and I want the values of the help vector in the original vector. I however only want to use 1 operation (so it should be done with a move, no copying of elements).

This is the code I use:

template<class T>
class A:public vector<T> {
    public:
        void fillAndMove();

        vector<T> help;
}

template<class T>
void A<T>:fillAndMove() {
    // Fill a help array with random values
    help.resize(2);
    help[0] = 5;
    help[1] = 3;

    // This line doesn't work
    *this = move(help);
}

I get following error:

no match for 'operator=' (operand types are 'A<int>' and 'std::remove_reference<std::vector<int, std::allocator<int> >&>::type {aka std::vector<int, std::allocator<int> >}')

I think the problem is that the help vector needs to be casted to a class A object but I don't know how I should do it. Anyone that can help me?

Aucun commentaire:

Enregistrer un commentaire