Re: warning C4238 : cast reference in void*

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Eric wrote:
CString foo2(CString aString)
{
return aString;
}
void* foo(CString &aString)
{
return (void*)&foo2(aString); //give C4238 warning
}
int main()
{
CString aString;
void* ptr = foo(aString);
}

foo2 returns temporary _copy_ of CString. So, you're trying to return an address of temporary object. This code will crash at runtime.
.



Relevant Pages