在php中,
$b=10;
返回的是一个中间值(值等于10),但不是$b;
程序:
function test(&$num){
$num=12;
}
test($a=10);
echo $a.'<br/>';
传入函数的是 = 运算符产生的返回值,这个返回值在test函数中被改变,但是不会影响函数外 $a 的值;
类似的程序在 c++中
#include <cstdlib>
#include <iostream>
using namespace std;
void test(int &num){
num=12;
}
int main(int argc, char *argv[])
{
int numB;
test(numB=10);
cout<<numB<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
调用test函数,作为参数传递到函数中的也是 numB=10 的返回值,但是在函数中对 numB的变更,引起了函数外 numB 的值的变化。这说明在C++中 = 运算符的返回值是被赋值的变量的地址;
本文探讨了PHP和C++中赋值运算符的行为差异,特别是在函数调用过程中。通过具体示例展示了当使用引用传递时,两种语言如何处理变量的赋值与修改。
5654

被折叠的 条评论
为什么被折叠?



