我们之前说过,c++是不支持在函数外返回函数内局部变量的地址的,除非其是static类型。
我们来看以下程序:
int *addition(int a ,int b)
{
int *sum=new int (a+b);
return sum;
}
C++的new操作符用于在堆上分配内存,不同于栈上的局部变量,new创建的空间在函数调用结束后仍可使用,因此可以用于向主函数传递参数。不过,使用new后要确保通过delete进行释放。
我们之前说过,c++是不支持在函数外返回函数内局部变量的地址的,除非其是static类型。
我们来看以下程序:
int *addition(int a ,int b)
{
int *sum=new int (a+b);
return sum;
}
630
2012
2349