search

Home  >  Q&A  >  body text

C++中函数的这个用法有些不解,望解惑,具体请见题干

迷茫迷茫2773 days ago423

reply all(3)I'll reply

  • 高洛峰

    高洛峰2017-04-17 14:44:24

    The f function returns a reference to a, and a is a global variable with a fixed address, so this reference can be used as an lvalue, that is, it can be assigned a value.
    This is a very tricky method, but it is not recommended. , because sometimes the variables you reference are defined very early, which is difficult for people who read this code to understand.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:44:24

    1. When an object is used as an rvalue, its content (value) is used. When it is used as an lvalue, its address is used.

    2. Here the variable a is a global variable, and the function f returns a reference to a.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:44:24

    Function f returns a reference to a, which is equivalent to directly assigning a value to a

    reply
    0
  • Cancelreply