return pointer to data declared in function

前端 未结 11 1621
南旧
南旧 2020-12-17 21:11

I know this won\'T work because the variable x gets destroyed when the function returns:

int* myFunction()
{
    int x = 4; return &x;
}
<
11条回答
  •  猫巷女王i
    2020-12-17 21:43

    For C++, in many cases, just return by value. Even in cases of larger objects, RVO will frequently avoid unnecessary copying.

提交回复
热议问题