按值传递数组 [英] Passing array by value

查看:68
本文介绍了按值传递数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将值数组传递给C ++函数?

Is it possible to pass an array by value to a C++ function?

如果是,我该怎么做?

推荐答案

如果用 array 表示原始 array ,那么答案是。您无法在C或C ++中按值传递它们。

If by array you mean raw array, then the answer is No. You cannot pass them by value either in C or C++.

两种语言都提供了一种解决方法,您可以在其中包装 struct 围绕该数组并传递其对象。示例:

Both the languages provide a workaround, where you can wrap a struct around that array and pass its object. Example:

struct MyArray { int a[100]; };
...
struct MyArray obj; // `obj.a[]` is the array of your interest
foo(obj);  // you have passed the `a[]` by value

传递了`a []`在C ++中,这样的结构很容易获得格式为 std :: vector std :: array

In C++, such structs are readily available in form of std::vector and std::array.

这篇关于按值传递数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆