fliker
fliker

Reputation: 47

void* to vector

I have a function from some big library. I must pass the argument as void*. I want to pass a vector. I'm passing it using

vector<myClass*> myName;

function(...,(void*)&myName,...)

Now in this function I want to cast void* back to vector but I don't know how to do this.

I'm trying something like:

vector<myClass*> myName = static_cast< vector<myClass*> >(voidPointerName);

but I get

error: invalid conversion from ‘void*’ to ‘long unsigned int’

error:   initializing argument 1 of ‘std::vector<_Tp, _Alloc>
            ::vector(size_t, const _Tp&, const _Alloc&)
            [with _Tp = myClass*, _Alloc = std::allocator<myClass*>]’

EDIT:

What I want to do is pass a vector of pointers to my own class to this function http://ftp.heanet.ie/disk1/www.gnu.org/software/libmicrohttpd/microhttpd/microhttpd_10.html, so I must cast it to void* and then cast it back to vector, so the code looks like:

    vector<myClass*> v
    MHD_create_response_from_callback (...,HERE_PASS_VECTOR,...);

and code for this function:

    callback(void* cls,...)
    {
      CAST_CLS_BACK_TO_VECTOR
      ITERATE_OVER_VECTOR
    }

Upvotes: 2

Views: 14176

Answers (3)

1982.venkat
1982.venkat

Reputation: 1

int a1=1, a2=2,a3=3,a4=4;

    std::vector<int> tst;

    tst.push_back(a1);
    tst.push_back(a2);
    tst.push_back(a3);
    tst.push_back(a4);
    for (std::vector<int>::iterator it = tst.begin() ; it != tst.end(); ++it)
    {
        std::cout << ' ' << *it;
        //ptst.push_back(const_cast<void*>(it));
    }
    std::cout << '\n';

    //const void *pitst; --WORKING

    //pitst=const_cast<void *>(reinterpret_cast<void *>(&a1)); --WORKING

    //std::cout<<"\n"<<*reinterpret_cast<const int *>(pitst); --WORKING

    std::vector<const void *> ptst;

    ptst.push_back(const_cast<void *>(reinterpret_cast<void *>(&a1)));
    ptst.push_back(const_cast<void *>(reinterpret_cast<void *>(&a2)));
    ptst.push_back(const_cast<void *>(reinterpret_cast<void *>(&a3)));
    ptst.push_back(const_cast<void *>(reinterpret_cast<void *>(&a4)));

    std::cout<<"\n"<< *reinterpret_cast<const int *>(ptst[0]);
    //std::cout<<"\n"<< *reinterpret_cast<const int *>(ptst.pop_back()); --compiler error

    for (std::vector<const void *>::iterator it = ptst.begin() ; it != ptst.end(); ++it)
    {
        std::cout<<"\n"<< *reinterpret_cast<const int *>(*it);
        //ptst.push_back(const_cast<void*>(it));
    }

...........

Output:

1 2 3 4

1 1 2 3 4 ..........

you can replace ..int with your ..class and try

Upvotes: 0

Mr.C64
Mr.C64

Reputation: 43014

This code of yours doesn't compile:

vector<MyClass*> myName = static_cast< vector<myClass*> >(voidPointerName);

Try to reason step by step.

You have a void* as input (voidPointerName), and you want a std::vector back.

The first thing you have to do is to cast from void* to vector*: you can use the modern C++-style cast reinterpret_cast for that purpose:

<<vector pointer>> p = reinterpret_cast< <<vector pointer>> >(voidPointerName);

Since you have a vector<MyClass*>, the <<vector pointer>> is actually vector<MyClass*> * (I just added the * for pointer).

So, the above pseudo code becomes:

vector<MyClass*>* p = reinterpret_cast<vector<MyClass*> *>(voidPointerName);

You may want to use a C++ reference & (which offers a value syntax with a pointer semantics), so your code can be slightly modified as:

vector<MyClass*>& v = *( << the reinterpret_cast thing above >> );

(On the right side of the above assignment, we dereferenced the pointer using *.)

i.e.

// Final form
vector<MyClass*>& v = *reinterpret_cast<vector<MyClass*> *>(voidPointerName);

Upvotes: 3

Some programmer dude
Some programmer dude

Reputation: 409442

I guess you're actually doing something like

vector<myClass*> msisdnStructs;
...
function(...,(void*)&msisdnStructs,...);

In that case you are passing a pointer to the vector. And in the function you are trying to convert that pointer-to-vector to a vector, which will of course not work. You can however dereference the passed void* argument (suitably casted), and use that to assign to a reference to a vector (using reference to avoid copying):

vector<myClass*>& myName = *reinterpret_cast<vector<myClass*>*>(voidPointerName);

Upvotes: 3

Related Questions