LPCVOID is a typedef, not a #define. And it doesn't have anything to do with templates. Win32 is a C interface, and templates are C++, not C.
Printable View
LPCVOID is a typedef, not a #define. And it doesn't have anything to do with templates. Win32 is a C interface, and templates are C++, not C.
i am not saying anything like you have assumed from my post :D
Oh. Then could you clarify?
consider this:
why we should use LPCVOID instead of const void *?
For type-matching with an API that specifies the use of such type.
Not really. You need some more practice, that's all. It removes long, template-ish lines which has to be duplicated all the time.
Quote:
but anyways such typedefs have advantages:
like:
Code:char* p, q r; // not all are pointers :(
LPSTR p, q, r; // all are pointers :)
It could go on. This will make your head hurt in complex code whereas, if you simply typed int, int*, void* or void**, it would be so much simpler.Code:typedef LPVOID void*;
typedef LPPVOID void**;
typedef LPINT int*;
void myfunc(LPVOID p);
int main()
{
LPVOID myvar;
LPPVOID myvarr;
myvarr = &myvar; /* Wtf? How can you assign the address of one variable to another which is not a pointer? */
int myint = 0;
LPINT myint2;
myint2 = &myint; /* Wtf? How can you assign the address of one variable to another which is not a pointer? */
*myint2 = 5; /* Wtf? Myint2 is a variable, not a pointer */
LPVOID myvar2 = 100; /* Valid in C! */
myfunc(myvar2, 4);
}
void myfunc(LPVOID p)
{
int val = 4;
memcpy(p, &val, sizeof(val));
}
Worse is if someone renames them such as:
Use documentation instead. It's much less error prone than using stupid typedefs for taking arguments.Code:typedef struct
{
/* ... */
} *mystruct;
/* Wtf? mystruct is a pointer, not a variable! */
Say: this function expects the address of a variable of type X instead of typing LPVOID.
Well in this case ...Quote:
For type-matching with an API that specifies the use of such type.
It would have been much better if the API used this:
instead of this ugliness:Code:void MSAPIFunction(char * s);
Code:typedef char * LPSTR;
void MSAPIFunction(LPSTR s);
Smart pointers aren't pointers either. Spotting that a type name starts with P or LP isn't really harder than spotting a * in the declaration. It's a very simple convention.Quote:
myvarr = &myvar; /* Wtf? How can you assign the address of one variable to another which is not a pointer? */
Not saying that I'd use it, but I don't see it decreasing readability.
It introduces confusion if abused. There have been examples of that with linked lists in the C forum multiple times.
At least it boggles my mind.
Win32 is an API, not an implementation. There might be another implementation on another platform which has different representations for variable types. Then what would you do if you wanted binary compatibility?
I think a better solution would be to have an API compatibility layer between the Win32 API and the code that you need to be in regular C types. Also consider whether you actually need Win32 types or C data types. I don't think Win32 data types would be a good choice for a linked list. Where's the connection?I thought I was...?Not a good mantra for computer programming. ;)
Of course it's fine to typedef types.
But it's bad to typedef pointers to types.
:|
Ah I see. But pointers are types. How do you know that the pointer types are the same?
typedef something mytype;
void foo(mytype* p);
This guarantees that it takes a pointer to something of type mytype.
So why the need for pointer typedefs?
Just as a quick clarification to Elysia, The only reason I even used LPCVOID was because he used LPVOID. Had he not used a win32 type I would have also not used a win32 type. Furthermore, I try to always match JAVA source with JAVA source, C++ source with C++ source, and even pseudo-code with pseudo-code. It represents one of two things happening, people getting an understandable answer to their question, or people wondering "why on earth did he post pseudo-code as an answer?" Thus inspiring them not to post it in the question :)
I see. Well, don't worry - I'm not blaming anyone (phew).
Friends again? :D