Converting (void*) to std::vector

前端 未结 4 2081
难免孤独
难免孤独 2021-01-07 22:31

I have a (void*) buffer that I need to convert to std::vector before I can pass it on. Unfortunately, my C++ casting skills a

4条回答
  •  忘掉有多难
    2021-01-07 23:09

    The only time this would be legitimate is if you had already created a vector, and simply wanted to get it back.

    void SomeFunc(void* input);
    
    main() {
      std::vector< unsigned char > v;
      SomeFunc((void*) &v);
    }
    
    SomeFunc(void* input) {
      // Now, you could cast that void* into a vector
      std::vector< unsigned char >* v_ = (vector*)input
    }
    

    I haven't actually tried to see if this will run, but that's the spirit of it. That said, if you are making this from scratch, you are definitely doing it wrong. This is really bad. The only time this could be even remotely understandable is if you are forced to implement the already defined "SomeFunc()".

提交回复
热议问题