C++ Pass By Const Reference and Return By Const Reference

后端 未结 5 1079
感动是毒
感动是毒 2021-01-11 18:23

I\'m trying to understand if there is any benefit to returning a const reference. I have a factorial function that normally looks like this:

uns         


        
5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-11 19:03

    This is invalid. You can't return reference to a local variable.

    MSVS C++ compiler even gives the following warning:

    main.cc : warning C4172: returning address of local variable or temporary
    

    Not quite sure about GCC, but probably the result would be the same.

提交回复
热议问题