简体   繁体   English

C ++将数组参数传递给函数

[英]C++ pass array argument to a function

I want to pass a reference to an array from one object GameModel to another PersonModel , store reference and then work with this array inside PersonModel just like inside GameModel, but... 我想PersonModel数组的引用从一个对象GameModel传递给另一个PersonModel ,存储引用,然后像在GameModel内一样在PersonModel内部使用此数组,但是...

I have a terrible misunderstanding of passing an array process: In the class PersonModel I want to pass an array by reference in a constructor (see code block below). 我对传递数组过程有一个严重的误解:在PersonModel类中,我想在构造函数中通过引用传递数组(请参见下面的代码块)。 But the marked line throws the compile error 但是标记的行会引发编译错误

    PersonModel::PersonModel( int path[FieldSize::HEIGHT][FieldSize::WIDTH], int permissionLevel ) { 
        this->path = path; //<------ ERROR here:
        //PersonModel.cpp:14:22: error: incompatible types in assignment of 'int (*)[30]' to 'int [31][30]'

        this->permissionLevel = permissionLevel;
    }

Here is the header file PersonModel.h 这是头文件PersonModel.h

#ifndef PERSON_MODEL
#define PERSON_MODEL

#include "data/FieldSize.h"

namespace game{
    class IntPosition;
    class MotionDirection;

    class PersonModel {
        protected:
        int path[FieldSize::HEIGHT][FieldSize::WIDTH];
        int permissionLevel;

        public:
        PersonModel( int path[FieldSize::HEIGHT][FieldSize::WIDTH], int permissionLevel );
        void setMotionDirection ( MotionDirection* md);
        void step(long time);
        void reset(long time);
    };
}

#endif 

As I see now, I can change the int path[FieldSize::HEIGHT][FieldSize::WIDTH]; 正如我现在看到的,我可以更改int path[FieldSize::HEIGHT][FieldSize::WIDTH]; declaration to int (*path)[FieldSize::WIDTH]; 声明为int (*path)[FieldSize::WIDTH]; but it is much more confusing. 但这更加令人困惑。

Help me understand this topic: what is the proper way to store the passed reference to an array to work with it later, like with usual 2D array. 帮助我理解此主题:将传递的引用存储到数组以供以后使用的正确方法是什么,就像通常的2D数组一样。

UPDATE: 更新:

This array is a map of game field tiles properties represented by bit-masks, so it is read-only actually. 该数组是由位掩码表示的游戏场图块属性的映射,因此它实际上是只读的。 All the incapsulated objects of GameModel class should read this array, but I definitely don't want to duplicate it or add some extra functionality. GameModel类的所有封装对象都应读取此数组,但是我绝对不想复制它或添加一些其他功能。

There are no frameworks just bare Android-NDK. 没有裸露的Android-NDK框架。

I think you've fallen into the classic trap of believing someone who's told you that "arrays and pointers are the same in C". 我认为您已经陷入了一个经典陷阱,即相信有人告诉您“ C语言中的数组和指针是相同的”。

The first thing I'd do would be to define a type for the array: 我要做的第一件事是为数组定义类型:

typedef int PathArray[FieldSize::HEIGHT][FieldSize::WIDTH];

You then don't need to worry about confusions between reference to array of ints vs array of references to ints. 然后,您无需担心引用整数数组与引用整数数组之间的混淆。

Your PersonModel then contains a reference to one of these. 然后,您的PersonModel包含对其中之一的引用

PathArray &path;

and, because its a reference it must be initialised in the constructors initialization list rather than in the constructor body. 并且,因为它是一个引用,所以必须在构造函数初始化列表中而不是在构造函数主体中进行初始化。

 PersonModel::PersonModel( PathArray &aPath, int aPermissionLevel ) :
   path(aPath),
   permissionLevel(aPermissionLevel)
 {
 }

Of course, holding references like this is a little scary so you might want to consider using a boost::shared_ptr or something similar instead to make the lifetime management more robust. 当然,持有这样的引用会有些吓人,因此您可能要考虑使用boost :: shared_ptr或类似的方法来使生命周期管理更可靠。

  1. You cannot assign arrays as you do with value types in C++ 您不能像在C ++中使用值类型那样分配数组
  2. int path[x][y] resolves to the type int (*)[y] int path [x] [y]解析为int(*)[y]类型

Possible solutions are: 可能的解决方案是:

  1. Using memcpy/copy 使用memcpy / copy
  2. Using std::array 使用std :: array

数组的名称是第一个元素的指针,因此,您可以尝试

PersonModel( int (*path)[FieldSize::HEIGHT][FieldSize::WIDTH], int permissionLevel );

You can't assign to an array like that. 您不能分配给这样的数组。 However you can use the fact that an array is a contiguous memory area, even when having an array of arrays, and use eg memcpy to copy the array: 但是,即使有数组数组,您也可以使用数组是连续内存区域的事实,并使用例如memcpy复制数组:

memcpy(this->path, path, FieldSize::HEIGHT * FieldSize::WIDTH * sizeof(int));

You would have to pass a pointer to the 2d-array as you cannot pass the array as you have stated in the code snippet. 您将必须传递一个指向2d数组的指针,因为您不能如代码片段中所述传递数组。 I would suggest using the STL array type. 我建议使用STL数组类型。 Admittedly std::array is C++ '11 standard and therefore old compiler may not support it. 诚然std :: array是C ++ '11标准,因此旧的编译器可能不支持它。 You can also use vector which has been around longer. 您也可以使用更长的向量。

vector<vector<int>>path;

You will have to resize the 2d-vector in the constructor. 您将必须在构造函数中调整2d向量的大小。 Indexing would look a bit funny: 索引看起来有点有趣:

path[1].[1] ....

With vectors, you can then pass it by reference. 对于矢量,您可以通过引用将其传递。

In C++ '=' implemented for primitive types like int and double but not for array(array is not a primitive type), so you should never use '=' to assign an array to new array, instead you should use something as memcpy to copy array. 在C ++中,为intdouble类的原始类型实现的'='而不是为array(array不是原始类型)的实现的,因此,您绝不应该使用'='将数组分配给新数组,而应该将memcpy用作复制数组。 memcpy copy a memory over another memory, so you can use it to copy an array over another array: memcpy在另一个内存上复制一个内存,因此您可以使用它在另一个数组上复制一个数组:

// memcpy( dst, src, size );
memcpy( this->path, path, FieldSize::HEIGHT * FieldSize * WEIGHT * sizeof(int) );

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM