简体   繁体   English

将结构和结构成员传递给功能

[英]Passing structures and structure members to functions

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. 并说我想将整个结构传递给f1,而只是将其传递给f2。 Would I use the data type a_struct as parameter for both? 我是否将数据类型a_struct用作两者的参数? Or would f2 have a different data type because I am only passing the member which is an int. 或者f2具有不同的数据类型,因为我只传递了一个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. 我认为这不会有太大的区别,只是它将自动通过引用传递。

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. 这将传递对a_struct对象的引用,并防止该对象的任何复制。 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: 结果,就足够了:

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];

For f1 to take an a_struct , it depends if you want to be able to edit it within f1 . 对于f1 ,要使用a_struct ,取决于您是否希望能够在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- const references. 我尝试避免使用非const引用。

If you want f2 to take an int , you write void f2(int); 如果要让f2接受一个int ,则编写void f2(int); . It doesn't care where that int comes from (ie inside a struct or on its own). 它并不关心int来源(即在struct内部还是struct )。 Then call it like f2(point.b); 然后称它为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. 遍历数组,然后将每个元素传递给采用a_struct的函数,或者传递整个数组(也带有size参数),然后在函数内部循环。 Ie you would do: 也就是说,您会这样做:

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 not an out of bounds access. 请注意,这不是超出范围的访问。 You are guaranteed to be allowed to make a pointer one 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). 当然,所有这些都假设您确实确实需要一个数组,而不是std::vector (在这种情况下,循环看起来基本相同,只是您将使用begin()end()成员函数)。

In fact, if you have access to c++11 you can use std::begin() and std::end() , which will work exactly the same for vector s or arrays. 实际上,如果可以访问c++11 ,则可以使用std::begin()std::end() ,它们对vector或数组的作用完全相同。

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 a in 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. 如果您需要传递数组,则其语法将与其他任何类型相同。

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作为引用,请使用指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 结构和功能(通过引用) - Structures and functions(passing by reference) 将结构成员传递给函数 - Passing structure members to a function 将void指针和结构传递给函数-c ++ - Passing void pointer and structures to functions -c++ 结构结构 - Structure of structures 通过值传递结构,并将另一个结构作为其成员之一,更改该成员的成员的值 - Passing a structure by value, with another structure as one of its members, changes values of this member's members 如果结构数组多于一个,如何用初始化列表定义结构变量的成员? - How do I define members of structure variable with an initialization list if there are more than one array of structures? 具有单个成员的自定义结构的集合(包装器)(也是定制结构),到单个成员的集合 - A collection of custom structures (a wrapper) with a single member (also a custom structure), to a collection of the single members 在lambda函数中捕获变量是静态数据结构的成员吗? - is variable capture in lambda functions which are members of a static data structure allowed? 将结构的枚举传递给其他函数并分配值 - Passing an enum of a structure to other functions and assigning the values 将任意结构存储的 void* 引用传递给函数 - Passing void* reference for arbitrary structure storage into functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM