简体   繁体   English

int to void * - 避免c风格演员?

[英]int to void* - avoiding c-style cast?

I need to cast an int (which specifies a byte offset) to a const void* . 我需要将一个int (指定一个字节偏移)转换为const void* The only solution that really works for me are c-style casts: 对我来说真正有用的唯一解决方案是c风格的演员表:

int offset = 6*sizeof(GLfloat);
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,(void*)offset);

I want to get rid of the c-style cast, but I dont't find a working solution. 我想摆脱c风格的演员,但我找不到一个有效的解决方案。 I tried 我试过了

static_cast<void*>(&offset)

and it compiles, but that cannot be the right solution (whole output is different with this approach). 它编译,但这不是正确的解决方案(整个输出与这种方法不同)。 What is here the right solution? 什么是正确的解决方案?

Link to documentation of glVertexAttribPointer : Link 链接到glVertexAttribPointer文档: 链接

Considering that it's a hopeless case (See the link provided by derhass), the probably best approach is to concentrate the questionable code in one place, tack an appropriately snide remark onto it and at least keep the remaining code clean: 考虑到它是一个绝望的情况(参见derhass提供的链接),最好的方法是将可疑代码集中在一个地方,对其进行适当的嘲讽评论,并至少保持其余代码清洁:

/**
 * Overload/wrapper around OpenGL's glVertexAttribIPointer, which avoids
 * code smell due to shady pointer arithmetic at the place of call.
 *
 * See also:
 * https://www.opengl.org/registry/specs/ARB/vertex_buffer_object.txt
 * https://www.opengl.org/sdk/docs/man/html/glVertexAttribPointer.xhtml
 */
void glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLsizei stride, size_t offset)
{
    GLvoid const* pointer = static_cast<char const*>(0) + offset;
    return glVertexAttribPointer(index, size, type, stride, offset);
}

Use this instead of void pointer: 使用此而不是void指针:

intptr_t 使用intptr_t

which is declared in header file file cstdint 在头文件文件cstdint中声明

and inter-convert from pointer by using 和使用指针进行交互转换

reinterpret_cast reinterpret_cast的

Read this in case of any problem: http://msdn.microsoft.com/en-us/library/e0w9f63b.aspx 如有任何问题,请阅读此内容: http//msdn.microsoft.com/en-us/library/e0w9f63b.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM