开发者

Arithmetic with void pointers in C++

开发者 https://www.devze.com 2023-02-12 00:43 出处:网络
I need to access an object in a buffer, pointed by a void pointer. The object开发者_高级运维 is located at a certain offset but since arithmetic on a void pointer is prohibited how can I access the ob

I need to access an object in a buffer, pointed by a void pointer. The object开发者_高级运维 is located at a certain offset but since arithmetic on a void pointer is prohibited how can I access the object?


You can cast the pointer to char* (+1 on such pointer is offset by one byte) or any other pointer type if that suits your needs better.

However, this approach is grossly error prone! You better check your design, something smells here! void* are in 99% of cases unnecessary in C++, designs that use them are usually more "C" than "C++". Remember, templates and inheritance should be the way to do these things.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号