Re: warning C4238 : cast reference in void*
- From: pj_hern@xxxxxxxxx
- Date: 2 Nov 2006 07:26:55 -0800
Eric wrote:
Ulrich Eckhardt wrote:
Eric wrote:
void foo(void* param)[...]
foo((void*) &szWord); //give C4238 warning : nonstandard extension
used : class rvalue used as lvalue
1. For your own sake, forget about the fact that C-style casts like the
above exist. I haven't found the need for one in over 1M lines of code and
you surely don't have such a case here.
Conversion from a typed pointer to a void pointer is implicitly done by the
compiler, so a cast isn't necessary. If you absolutely want, use a
static_cast, which is also the right cast to convert to a typed pointer
again.
2. Igor already mentioned it, this can't be the code you tried to compile,
please show the real code for an analysis of its problems.
Uli
Thanks all, sorry I didn't give you the good code.
CString foo2(CString aString)
{
return aString;
}
void* foo(CString &aString)
{
return (void*)&foo2(aString); //give C4238 warning
Thats like shooting yourself in the foot: twice. foo2 returns a
temporary and foo returns a typeless pointer to a local temporary. Its
so unfortunate that the compiler generates only a warning in this case
although the programmer should see the issue instantly. Void pointers
are deceptive enough but a void pointer to garbage is brain dead logic
(sorry).
Please use a static_cast instead of prehistoric casting mechanism. The
above 2 functions can be replaced by:
CString s;
void* p = static_cast<void*>(&s);
There is no way i can explain how inherently dangerous and deeply
fallacious those 2 functions are.
They both ignore scope and lifetimes flagrantly. They are plain evil.
Black Holes(c).
}
int main()
{
CString aString;
void* ptr = foo(aString);
Is basicly *guarenteed* to point to nothing.
}
Eric
.
- References:
- warning C4238 : cast reference in void*
- From: Eric
- Re: warning C4238 : cast reference in void*
- From: Ulrich Eckhardt
- Re: warning C4238 : cast reference in void*
- From: Eric
- warning C4238 : cast reference in void*
- Prev by Date: Re: warning C4238 : cast reference in void*
- Next by Date: Re: const ref vs. pointer
- Previous by thread: Re: warning C4238 : cast reference in void*
- Next by thread: Slowdown because of exceptions?
- Index(es):
Relevant Pages
|