In my main function I create an array of objects of a certain class "Menu"
在我的main函数中,我创建了一个特定类“Menu”的对象数组
And when I call a function I want to provide a pointer to that array.
当我调用一个函数时,我想提供一个指向该数组的指针。
Menu menu[2];
// Create menu [0], [1]
Function(POINTER_TO_ARRAY);
Question: What is the correct way to write the Function parameters?
问题:编写Function参数的正确方法是什么?
I try:
Function(&menu);
and in Header file:
并在头文件中:
void Function(Menu *menu[]); // not working
error: Cannot convert parameter 1 from Menu(*)[2] to Menu *[]
void Function(Menu * menu); // not working
error: Cannot convert parameter 1 from Menu(*)[2] to Menu *[]
and I can't come up with any other way to do this and I can't find a solution to this particular problem.
我无法想出任何其他方法来做到这一点,我无法找到解决这一特定问题的方法。
Simply, I want to be able to access the Menu array within the function through a pointer. What are the difference in normal pointer to a pointer to an array?
简单地说,我希望能够通过指针访问函数内的Menu数组。指向数组的指针的普通指针有什么区别?
4 个解决方案
#1
9
Declaration:
void Function(Menu* a_menus); // Arrays decay to pointers.
Invocation:
Function(menu);
However, you would need to inform Function()
how many entries are in the array. As this is C++ suggest using std::array
or std::vector
which have knowledge of their size, beginning and end:
但是,您需要通知Function()数组中有多少条目。因为这是C ++建议使用std :: array或std :: vector,它们知道它们的大小,开头和结尾:
std::vector<Menu> menus;
menus.push_back(Menu("1"));
menus.push_back(Menu("2"));
Function(menus);
void Function(const std::vector<Menu>& a_menus)
{
std::for_each(a_menus.begin(),
a_menus.end(),
[](const Menu& a_menu)
{
// Use a_menu
});
}
#2
3
Either by const or non-const pointer
通过const或非const指针
void Function(Menu const* menu);
void Function(Menu* menu);
...or by const or non-const reference
...或通过const或非const引用
void Function(Menu const (&menu)[2]);
void Function(Menu (&menu)[2]);
which can be generalized to a template so that the array size will be deduced by the compiler:
可以推广到模板,以便编译器推导出数组大小:
template<size_t N> void Function(Menu const (&menu)[N]);
template<size_t N> void Function(Menu (&menu)[N]);
Always call as Function(menu);
始终调用函数(菜单);
#3
3
Should work if you use
如果你使用,应该工作
void Function(Menu * menu);
and call using
并使用
Function(menu);
instead of
Function(&menu);
passing the array name causes it to decay to a pointer to the type contained in the array. However, as @hmjd says in his answer you will also need to pass the array size, so his suggestion of using a vector is favourable if this option is open to you.
传递数组名称会导致它衰减为指向数组中包含的类型的指针。但是,正如@hmjd在他的回答中所说,你还需要传递数组大小,所以如果这个选项对你开放,他建议使用向量是有利的。
#4
0
You can use
您可以使用
Function((void *) whatever_pointer_type_or_array_of_classes);
in your main.
在你的主要。
And in the function:
并在功能:
type Function(void * whatever)
{
your_type * x =(your_type *) whatever;
//use x
....
x->file_open=true;
....
}
#1
9
Declaration:
void Function(Menu* a_menus); // Arrays decay to pointers.
Invocation:
Function(menu);
However, you would need to inform Function()
how many entries are in the array. As this is C++ suggest using std::array
or std::vector
which have knowledge of their size, beginning and end:
但是,您需要通知Function()数组中有多少条目。因为这是C ++建议使用std :: array或std :: vector,它们知道它们的大小,开头和结尾:
std::vector<Menu> menus;
menus.push_back(Menu("1"));
menus.push_back(Menu("2"));
Function(menus);
void Function(const std::vector<Menu>& a_menus)
{
std::for_each(a_menus.begin(),
a_menus.end(),
[](const Menu& a_menu)
{
// Use a_menu
});
}
#2
3
Either by const or non-const pointer
通过const或非const指针
void Function(Menu const* menu);
void Function(Menu* menu);
...or by const or non-const reference
...或通过const或非const引用
void Function(Menu const (&menu)[2]);
void Function(Menu (&menu)[2]);
which can be generalized to a template so that the array size will be deduced by the compiler:
可以推广到模板,以便编译器推导出数组大小:
template<size_t N> void Function(Menu const (&menu)[N]);
template<size_t N> void Function(Menu (&menu)[N]);
Always call as Function(menu);
始终调用函数(菜单);
#3
3
Should work if you use
如果你使用,应该工作
void Function(Menu * menu);
and call using
并使用
Function(menu);
instead of
Function(&menu);
passing the array name causes it to decay to a pointer to the type contained in the array. However, as @hmjd says in his answer you will also need to pass the array size, so his suggestion of using a vector is favourable if this option is open to you.
传递数组名称会导致它衰减为指向数组中包含的类型的指针。但是,正如@hmjd在他的回答中所说,你还需要传递数组大小,所以如果这个选项对你开放,他建议使用向量是有利的。
#4
0
You can use
您可以使用
Function((void *) whatever_pointer_type_or_array_of_classes);
in your main.
在你的主要。
And in the function:
并在功能:
type Function(void * whatever)
{
your_type * x =(your_type *) whatever;
//use x
....
x->file_open=true;
....
}