提问者:小点点

C++通过引用传递引用类型参数


void f1(t a);
void f2(t &a);

int main(){
  t a;
  f1(a);
  f2(&a);
  f1(&a);
  f2(a);
}

有人能详细解释一下Main中的四个函数调用有什么区别吗?


共1个答案

匿名用户

void f1(t a);
void f2(t &a);

int main(){
  t a; //create a variable of type t;
  f1(a);//pass the value of a to the function f1
  f2(&a);//pass the address of a to the function f2 but, this function has a reference as a parameter so it shouldn't compile, you should pass the variable f2(a)
  f1(&a); // pass the address of a to the function f1, so f1 should contain a pointer as a parameter, but that's not the case, so it won't compile
  f2(a); //passing by reference to the function f2
}

请登录https://www.onlinegdb.com/

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|引用|传递|引用|类型|参数)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?