I like to be more standard as possible, so why should I \"constrain\" my classes defining it\'s members as OpenGL types when I can use primitive types? Is there any advantag
The type "unsigned int" has a different size depending on the platform you're building on. I expect this to normally be 32 bits, however it could be 16 or 64 (or something else -- depending on the platform).
Library-specific types are often created to be typedef'd according to platform-specific rules. This allows a generic application to use the right type without having to be aware of the platform it will be built for. Instead, the platform-specific knowledge is constrained to a single common header file.
Better cross-platform compatibility.
The advantages has already been mentioned here. However, there is a disadvantage clear from the following examples:
class FileIn
{
public:
//Public interface like read
private:
void* handle;
};
The above code fits very well in a platform independent header but writing
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
class FileIn
{
public:
//Public interface like read
private:
HANDLE handle;
};
does not.
Though the former will require ugly typecasts like
int fd=(int)( (size_t)handle );
close(fd);
i do not know any system which have sizeof(void*)
< sizeof(int)
. Yes it will fail if open returns a negative number for a valid file handle.
What to learn about this? Avoid using typedefs in library include files. Instead use struct declarations even though C programmers need to write struct
a dozen times. Here, some C standard library implementations do it all wrong.
In stdio.h
:
struct FILE;
And in the application:
struct FILE* the_file=fopen("filename.txt","rb");
/*...*/
In stdio.h
:
typedef struct SOMENAMETHATNOONESHOULDUSE
{
/* Internal data members */
} FILE;
In application
FILE* the_file=fopen("filename.txt","rb");
When writing a C++ wrapper, this forces either #include <cstdio>
or simply declare the handle as above.
i don't think it matters in this case because the spec says they are minimum sizes, not strict sizes. have a look at gl.h ~line 149 they're just typedefs of basic C types. they are just a convenience - for example there is a boolean type, so if you're using C89 and don't use any booleans then there's one set up for you to use with GL. GLuint is just a shorter way of typing unsigned int:
typedef unsigned int GLenum;
typedef unsigned char GLboolean;
typedef unsigned int GLbitfield;
typedef void GLvoid;
typedef signed char GLbyte; /* 1-byte signed */
typedef short GLshort; /* 2-byte signed */
typedef int GLint; /* 4-byte signed */
typedef unsigned char GLubyte; /* 1-byte unsigned */
typedef unsigned short GLushort; /* 2-byte unsigned */
typedef unsigned int GLuint; /* 4-byte unsigned */
typedef int GLsizei; /* 4-byte signed */
typedef float GLfloat; /* single precision float */
typedef float GLclampf; /* single precision float in [0,1] */
typedef double GLdouble; /* double precision float */
typedef double GLclampd; /* double precision float in [0,1] */