Returning a reference can work?

后端 未结 3 1603
天涯浪人
天涯浪人 2021-01-26 05:35

I used to think returning a reference is bad as our returned reference will refer to some garbage value. But this code works (matrix is a class):

co         


        
3条回答
  •  旧时难觅i
    2021-01-26 05:53

    I see no buf declared anywhere, which means it doesn't go out of scope with function return, so it's okay. (it actually looks like it's meant to be matrixbuf which is also fine, because it's static).

    EDIT: Thanks to R. Martinho Fernandes for the guess. Of course it is matrix buf, so it makes buf static array in which temporary is allocated to make sure it doesn't get freed when the function returns and therefore the return value is still valid.

提交回复
热议问题