Re: warning C4238 : cast reference in void*

Tech-Archive recommends: Fix windows errors by optimizing your registry




Alex Blekhman wrote:
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.

Sorry all, I understand that local variable are temporary objet. My
question was more about the warning C4238 and the class rvalue, lvalue
issue.

why void* ptr = &foo2(aString) give this warning ?

Regards,
Eric

.



Relevant Pages

  • Re: warning C4238 : cast reference in void*
    ... void* foo(CString &aString) ... question was more about the warning C4238 and the class rvalue, ... why void* ptr = &foo2give this warning? ...
    (microsoft.public.vc.language)
  • Re: warning C4238 : cast reference in void*
    ... Conversion from a typed pointer to a void pointer is implicitly done by the ... CString foo2(CString aString) ... CString aString; ...
    (microsoft.public.vc.language)
  • Re: warning C4238 : cast reference in void*
    ... void* foo(CString &aString) ... void* ptr = foo; ... to return an address of temporary object. ...
    (microsoft.public.vc.language)
  • Re: (void**)&ppval
    ... a sort of "generic data pointer type". ... returned from mallochas type "void *", so that it can point to ... void *tmp; ... T *ptr; ...
    (comp.lang.c)
  • Re: warning C4238 : cast reference in void*
    ... void* foo ... why void* ptr = &foo2give this warning? ... Do you understand what rvalue and lvalue mean? ...
    (microsoft.public.vc.language)