#include<string.h>
#include<limits.h>
using namespace std;
void v6(char rq,int &cost)
{
if(rq=='2')
cost+=1;
if(rq=='1')
cost+=2;
if(rq=='3')
cost+=3;
}
int main()
{
int cost=0;
v6(2,cost);
cout<<cost;
}
输出:
0
但是,c
的值(通过引用传递)不会改变;请解释如何。
也用过指针,但没用
最佳答案
我对你的函数调用稍作改动应该会给你你想要的行为
v6('2',cost);
您的 if
语句检查 char
而不是数字。您要么应用上述更改,要么更改 if
语句
void v6(char rq,int &cost)
{
if(rq==2)
cost+=1;
if(rq==1)
cost+=2;
if(rq==3)
cost+=3;
}
关于c++ - 通过引用传递后值不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55720785/