Re: warning C4238 : cast reference in void*
- From: pj_hern@xxxxxxxxx
- Date: 1 Nov 2006 20:51:16 -0800
Eric wrote:
Hi,
I want to pass a reference of an object as the parameter of a function.
This function takes an void*
Thats not a reference, its an address. And the trick is to cast the
receiving pointer. Note that a cast does *not* modify the casted
object. See below.
void foo(void* param)
{
...
}
int main()
{
CString szWord;
foo((void*) &szWord); //give C4238 warning : nonstandard extension
used : class rvalue used as lvalue
}
How can I solve this warning ?
void foo(void* param)
{
CString* ps = (CString*)param;
// use *ps
// or better
CString* psc = static_cast<CString*>(param);
// use *psc
}
int main()
{
CString word = "a string";
foo((void*)&word);
// or
foo(static_cast<void*>(&word));
return 0;
}
If you want foo() to take the pointer safely
void foo(void* const param) // a constant pointer to void
{
CString* ps = (CString*)param;
// param = 0; illegal
}
.
- References:
- warning C4238 : cast reference in void*
- From: Eric
- warning C4238 : cast reference in void*
- Prev by Date: Re: VC++ 6.0, 7.0, and 8.0 Compatibility
- Next by Date: const ref vs. pointer
- Previous by thread: Re: warning C4238 : cast reference in void*
- Next by thread: Re: warning C4238 : cast reference in void*
- Index(es):
Relevant Pages
|