我知道如何通过引用调用和通过指针操作调用。但我对在增量操作前和增量后同时执行这两种操作感到困惑。这里是代码片段。
使用指针参数通过引用调用
void fun2(int *x) // It works like int *x=&x;
{
++*x; // *x++ does not provide lvalue, so will not work here [ I think So ]
}
使用引用参数通过引用调用
void fun3(int& x)
{
++x; // x++; [both works fine but I can't understand why?]
}
这是驱动程序代码
int main()
{
int x=10;
cout<<x<<"\n"; //10
fun2(&x);
cout<<x<<"\n"; //11
fun3(x);
cout<<x<<"\n"; //12
return 0;
}
为什么在 fun2() *x++ 中输出 10 而不是 11 但 ++*x 工作正常,而在 fun3() 中 x++ 和 ++x 工作正常?