Int to void * - avoid c-style cast?

I need to point int(which determines the byte offset) to const void*. The only solution that really works for me is c-style casts:

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

I want to get rid of c-style cast, but I will not find a working solution. I tried

static_cast<void*>(&offset)

and it compiles, but this may not be the right solution (the whole conclusion is different from this approach). What is the right solution here?

Documentation linkglVertexAttribPointer : Link

+4
source share
2 answers

, (. , derhass), , , , :

/**
 * Overload/wrapper around OpenGL 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);
}
+3

void:

intptr_t

cstdint

inter-convert from pointer

reinterpret_cast

- : http://msdn.microsoft.com/en-us/library/e0w9f63b.aspx

+2

All Articles