C++ 将结构和结构成员传递给函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13624353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:33:12  来源:igfitidea点击:

Passing structures and structure members to functions

c++arraysstructuremembers

提问by user1768079

When passing structures to functions what would the prototype / header look like? What would they look like when passing members of structures to functions?

将结构传递给函数时,原型/标头会是什么样子?将结构成员传递给函数时,它们会是什么样子?

For example...

例如...

struct a_struct{
int a;
int b;
};

a_struct point;

void f1(a_struct)
void f2(a_struct)

And lets say that I want to pass the whole structure to f1, but just a member to f2. Would I use the data type a_struct as parameter for both? Or would f2 have a different data type because I am only passing the member which is an int. Would this vary for an array of structures? The program I have been tasked to write is supposed to use arrays of structures. I figured that this wouldn't make much of a difference except that it will be passed by reference automatically.

假设我想将整个结构传递给 f1,但只是将一个成员传递给 f2。我会使用数据类型 a_struct 作为两者的参数吗?或者 f2 会有不同的数据类型,因为我只传递一个 int 成员。对于结构数组,这会有所不同吗?我负责编写的程序应该使用结构数组。我认为这不会有太大区别,只是它会自动通过引用传递。

回答by ScoPi

When passing objects around (not just scalar values), you have to be concerned about memory ownership and copying, etc. This means that you have multiple options for how you declare the interface for your function. For example, you can pass by reference or pass by value.

当传递对象(不仅仅是标量值)时,您必须关心内存所有权和复制等。这意味着您有多种选择如何为您的函数声明接口。例如,您可以按引用传递或按值传递。

A possible declaration for you might be:

您可能的声明可能是:

void f1(a_struct& my_struct);

This would pass a reference to the a_struct object and prevent any copying of the object. However, your example structure just contains scalar values, so the possibility of copying the object isn't cause for too much worry. As a result, this would suffice:

这将传递对 a_struct 对象的引用并防止对该对象进行任何复制。但是,您的示例结构仅包含标量值,因此复制对象的可能性不必太担心。结果,这就足够了:

void f1(a_struct my_struct);

As far as passing a member of the struct into a function, the function would need to be declared to take the type of the member. For example, to pass the a member of the a_struct into a function, you would do:

至于将结构的成员传递给函数,则需要声明该函数以采用该成员的类型。例如,要将 a_struct 的成员传递给函数,您可以执行以下操作:

void f1(int val);

Finally, arrays do complicate things as they would come in as a pointer to the function. For example, to pass an array of a_struct objects, you would make this declaration:

最后,数组确实使事情变得复杂,因为它们会作为指向函数的指针出现。例如,要传递一个 a_struct 对象数组,您可以进行以下声明:

void f1(a_struct* my_struct);

However, you could then simply reference the parameter normally. For example:

但是,您随后可以简单地正常引用该参数。例如:

my_structs[1];

回答by BoBTFish

For f1to take an a_struct, it depends if you want to be able to edit it within f1. void f1(a_struct s);will work fine, and make a copy. void f1(const a_struct & s);takes a reference, but you can't change it (without some hassle anyway). I try to avoid non-constreferences.

对于f1采取的a_struct,它如果你想能中编辑它依赖f1void f1(a_struct s);将工作正常,并制作副本。void f1(const a_struct & s);需要参考,但您无法更改它(无论如何都没有麻烦)。我尽量避免非const引用。

If you want f2to take an int, you write void f2(int);. It doesn't care where that intcomes from (i.e. inside a structor on its own). Then call it like f2(point.b);

如果你f2想拿一个int,你写void f2(int);。它不关心它int来自哪里(即在 a 内部struct或它自己)。然后称之为f2(point.b);

Functions that take arrays can be troublesome, it depends what you want to do with it. The simplest way is to remember that an array degenerates to a pointer.

采用数组的函数可能很麻烦,这取决于您想用它做什么。最简单的方法是记住数组退化为指针。

void f3 (a_struct * const structArray)
{ /* Do something */ }
a_struct myArray[10];
f3(myArray);

There are also other (better) ways to do this.

还有其他(更好的)方法可以做到这一点。

Edit: If you want to apply a function to every element of an array, there are (superficially) two approaches (which if you think about it, are only really one approach). Loop over the array, and pass each element to a function that takes an a_struct, or pass the whole array in (with a size parameter as well), and loop inside the function. I.e. you would do:

编辑:如果你想对数组的每个元素应用一个函数,有(表面上)两种方法(如果你考虑一下,这只是一种方法)。循环遍历数组,并将每个元素传递给一个接受 的函数a_struct,或者传递整个数组(也带有一个 size 参数),然后在函数内部循环。即你会这样做:

a_struct myArray[10];
for (int i = 0; i < 10; ++i)
{
    f1( myArray[i] );
}

// Or, better, from <algorithm>
std::for_each(myArray, myArray+10, f1);

Note this is notan out of bounds access. You are guaranteed to be allowed to make a pointerone off the end of an array (exactly for these looping situations), as long as it is never dereferenced.

请注意,这不是越界访问。只要永远不会取消引用,您就可以保证在数组末尾创建一个指针(正是针对这些循环情况)。

Of course, this all assumes that you really do want an array, not a std::vector(in which case the loops look basically the same, except you would use the begin()and end()member functions).

当然,这一切都假设您确实想要一个数组,而不是 a std::vector(在这种情况下,循环看起来基本相同,除非您将使用begin()andend()成员函数)。

In fact, if you have access to c++11you can use std::begin()and std::end(), which will work exactly the same for vectors or arrays.

事实上,如果您有权访问,则c++11可以使用std::begin()and std::end(),这对于vectors 或数组的工作方式完全相同。

回答by ApplePie

When you want to pass a structure (or a class):

当你想传递一个结构(或一个类)时:

return_type fn_name(object_name)

When you want to pass one of its members, let's say ain this case.

当你想通过它的一个成员时,让我们a在这种情况下说。

return_type fn_name(int)

Also, unless I am mistaken, all functions parameters default to pass by value unless you specify otherwise explicitly. If you need to pass arrays then it would be the same syntax as with any other types.

此外,除非我弄错了,否则所有函数参数默认为按值传递,除非您明确指定。如果您需要传递数组,那么它的语法与任何其他类型的语法相同。

回答by Debobroto Das

if you want to pass the value of just a member function send its type for example a function which takes 'a' of a_struct as paramaeter should be void func(int a);

如果您只想传递成员函数的值,请发送其类型,例如将 a_struct 的“a”作为参数的函数应该是 void func(int a);

but it will work for call by value. If you want to use call be reference use the pointer.

但它适用于按价值调用。如果你想使用 call be reference 使用指针。