C ++从函数返回多维数组

时间:2022-04-18 09:56:01

How do I return a multidimensional array hidden in a private field?

如何返回隐藏在私有字段中的多维数组?

class Myclass {
private:
 int myarray[5][5];
public:
 int **get_array();
};

........

int **Myclass::get_array() {
 return myarray;
}

cannot convert int (*)[5][5] to int** in return test.cpp /Polky/src line 73 C/C++ Problem

无法将int(*)[5] [5]转换为int **返回test.cpp / Polky / src第73行C / C ++问题

7 个解决方案

#1


22  

A two-dimensional array does not decay to a pointer to pointer to ints. It decays to a pointer to arrays of ints - that is, only the first dimension decays to a pointer. The pointer does not point to int pointers, which when incremented advance by the size of a pointer, but to arrays of 5 integers.

二维数组不会衰减到指向int的指针。它衰减到指向int数组的指针 - 也就是说,只有第一个维度衰减到指针。指针不指向int指针,当指针的大小增加时,它指向5个整数的数组。

class Myclass {
private:
    int myarray[5][5];
public:
    typedef int (*pointer_to_arrays)[5]; //typedefs can make things more readable with such awkward types

    pointer_to_arrays get_array() {return myarray;}
};

int main()
{
    Myclass o;
    int (*a)[5] = o.get_array();
    //or
    Myclass::pointer_to_arrays b = o.get_array();
}

A pointer to pointer (int**) is used when each subarray is allocated separately (that is, you originally have an array of pointers)

当每个子数组分别分配时(即,您最初有一个指针数组),使用指向指针(int **)的指针

int* p[5];
for (int i = 0; i != 5; ++i) {
    p[i] = new int[5];
}

Here we have an array of five pointers, each pointing to the first item in a separate memory block, altogether 6 distinct memory blocks.

这里我们有一个包含五个指针的数组,每个指针指向一个单独的内存块中的第一个项目,共有6个不同的内存块。

In a two-dimensional array you get a single contiguous block of memory:

在二维数组中,您将获得一个连续的内存块:

int arr[5][5]; //a single block of 5 * 5 * sizeof(int) bytes

You should see that the memory layout of these things are completely different, and therefore these things cannot be returned and passed the same way.

您应该看到这些东西的内存布局完全不同,因此这些东西不能以相同的方式返回和传递。

#2


16  

There are two possible types that you can return to provide access to your internal array. The old C style would be returning int *[5], as the array will easily decay into a pointer to the first element, which is of type int[5].

您可以返回两种可能的类型来提供对内部数组的访问。旧的C样式将返回int * [5],因为数组很容易衰变成指向第一个元素的指针,该元素的类型为int [5]。

int (*foo())[5] {
   static int array[5][5] = {};
   return array;
}

Now, you can also return a proper reference to the internal array, the simplest syntax would be through a typedef:

现在,您还可以返回对内部数组的正确引用,最简单的语法是通过typedef:

typedef int (&array5x5)[5][5];
array5x5 foo() {
   static int array[5][5] = {};
   return array;
}

Or a little more cumbersome without the typedef:

或者没有typedef会更麻烦:

int (&foo())[5][5] {
   static int array[5][5] = {};
   return array;
}

The advantage of the C++ version is that the actual type is maintained, and that means that the actual size of the array is known at the callers side.

C ++版本的优点是保持了实际类型,这意味着在调用者端已知数组的实际大小。

#3


5  

To return a pointer to your array of array member, the type needed is int (*)[5], not int **:

要返回指向数组成员数组的指针,需要的类型是int(*)[5],而不是int **:

class Myclass {
private:
    int myarray[5][5];
public:
    int (*get_array())[5];
};

int (*Myclass::get_array())[5] {
    return myarray;
}

#4


2  

How do I return a multidimensional array hidden in a private field?

如何返回隐藏在私有字段中的多维数组?

If it's supposed to be hidden, why are you returning it in the first place?

如果它应该被隐藏,你为什么要首先归还它?

Anyway, you cannot return arrays from functions, but you can return a pointer to the first element. What is the first element of a 5x5 array of ints? An array of 5 ints, of course:

无论如何,您不能从函数返回数组,但您可以返回指向第一个元素的指针。 5x5整数数组的第一个元素是什么?一组5个整数,当然:

int (*get_grid())[5]
{
    return grid;
}

Alternatively, you could return the entire array by reference:

或者,您可以通过引用返回整个数组:

int (&get_grid())[5][5]
{
    return grid;
}

...welcome to C declarator syntax hell ;-)

...欢迎来到C声明者语法地狱;-)

May I suggest std::vector<std::vector<int> > or boost::multi_array<int, 2> instead?

我可以建议使用std :: vector >或boost :: multi_array 吗? ,2>

#5


1  

I managed to make this function work in C++0x using automatic type deduction. However, I can't make it work without that. Native C arrays are not supported very well in C++ - their syntax is exceedingly hideous. You should use a wrapper class.

我设法使用自动类型推导使此功能在C ++ 0x中工作。但是,如果没有这个,我就无法工作。在C ++中不能很好地支持本机C数组 - 它们的语法非常可怕。你应该使用包装类。

template<typename T, int firstdim, int seconddim> class TwoDimensionalArray {
    T data[firstdim][seconddim];
public:
    T*& operator[](int index) {
        return data[index];
    }
    const T*& operator[](int index) const {
        return data[index];
    }
};
class Myclass {
public:
    typedef TwoDimensionalArray<int, 5, 5> arraytype;
private:
    arraytype myarray;
public:
    arraytype& get_array() {
        return myarray;
    }
};

int main(int argc, char **argv) {
    Myclass m;
    Myclass::arraytype& var = m.get_array();
    int& someint = var[0][0];
}

This code compiles just fine. You can get pre-written wrapper class inside Boost (boost::array) that supports the whole shebang.

这段代码编译得很好。你可以在支持整个shebang的Boost(boost :: array)中获得预先编写的包装类。

#6


-1  

Change your int's to int[][]'s or try using int[,] instead?

将你的int更改为int [] []或尝试使用int [,]而不是?

#7


-1  

int **Myclass::get_array() { 
 return (int**)myarray; 
} 

#1


22  

A two-dimensional array does not decay to a pointer to pointer to ints. It decays to a pointer to arrays of ints - that is, only the first dimension decays to a pointer. The pointer does not point to int pointers, which when incremented advance by the size of a pointer, but to arrays of 5 integers.

二维数组不会衰减到指向int的指针。它衰减到指向int数组的指针 - 也就是说,只有第一个维度衰减到指针。指针不指向int指针,当指针的大小增加时,它指向5个整数的数组。

class Myclass {
private:
    int myarray[5][5];
public:
    typedef int (*pointer_to_arrays)[5]; //typedefs can make things more readable with such awkward types

    pointer_to_arrays get_array() {return myarray;}
};

int main()
{
    Myclass o;
    int (*a)[5] = o.get_array();
    //or
    Myclass::pointer_to_arrays b = o.get_array();
}

A pointer to pointer (int**) is used when each subarray is allocated separately (that is, you originally have an array of pointers)

当每个子数组分别分配时(即,您最初有一个指针数组),使用指向指针(int **)的指针

int* p[5];
for (int i = 0; i != 5; ++i) {
    p[i] = new int[5];
}

Here we have an array of five pointers, each pointing to the first item in a separate memory block, altogether 6 distinct memory blocks.

这里我们有一个包含五个指针的数组,每个指针指向一个单独的内存块中的第一个项目,共有6个不同的内存块。

In a two-dimensional array you get a single contiguous block of memory:

在二维数组中,您将获得一个连续的内存块:

int arr[5][5]; //a single block of 5 * 5 * sizeof(int) bytes

You should see that the memory layout of these things are completely different, and therefore these things cannot be returned and passed the same way.

您应该看到这些东西的内存布局完全不同,因此这些东西不能以相同的方式返回和传递。

#2


16  

There are two possible types that you can return to provide access to your internal array. The old C style would be returning int *[5], as the array will easily decay into a pointer to the first element, which is of type int[5].

您可以返回两种可能的类型来提供对内部数组的访问。旧的C样式将返回int * [5],因为数组很容易衰变成指向第一个元素的指针,该元素的类型为int [5]。

int (*foo())[5] {
   static int array[5][5] = {};
   return array;
}

Now, you can also return a proper reference to the internal array, the simplest syntax would be through a typedef:

现在,您还可以返回对内部数组的正确引用,最简单的语法是通过typedef:

typedef int (&array5x5)[5][5];
array5x5 foo() {
   static int array[5][5] = {};
   return array;
}

Or a little more cumbersome without the typedef:

或者没有typedef会更麻烦:

int (&foo())[5][5] {
   static int array[5][5] = {};
   return array;
}

The advantage of the C++ version is that the actual type is maintained, and that means that the actual size of the array is known at the callers side.

C ++版本的优点是保持了实际类型,这意味着在调用者端已知数组的实际大小。

#3


5  

To return a pointer to your array of array member, the type needed is int (*)[5], not int **:

要返回指向数组成员数组的指针,需要的类型是int(*)[5],而不是int **:

class Myclass {
private:
    int myarray[5][5];
public:
    int (*get_array())[5];
};

int (*Myclass::get_array())[5] {
    return myarray;
}

#4


2  

How do I return a multidimensional array hidden in a private field?

如何返回隐藏在私有字段中的多维数组?

If it's supposed to be hidden, why are you returning it in the first place?

如果它应该被隐藏,你为什么要首先归还它?

Anyway, you cannot return arrays from functions, but you can return a pointer to the first element. What is the first element of a 5x5 array of ints? An array of 5 ints, of course:

无论如何,您不能从函数返回数组,但您可以返回指向第一个元素的指针。 5x5整数数组的第一个元素是什么?一组5个整数,当然:

int (*get_grid())[5]
{
    return grid;
}

Alternatively, you could return the entire array by reference:

或者,您可以通过引用返回整个数组:

int (&get_grid())[5][5]
{
    return grid;
}

...welcome to C declarator syntax hell ;-)

...欢迎来到C声明者语法地狱;-)

May I suggest std::vector<std::vector<int> > or boost::multi_array<int, 2> instead?

我可以建议使用std :: vector >或boost :: multi_array 吗? ,2>

#5


1  

I managed to make this function work in C++0x using automatic type deduction. However, I can't make it work without that. Native C arrays are not supported very well in C++ - their syntax is exceedingly hideous. You should use a wrapper class.

我设法使用自动类型推导使此功能在C ++ 0x中工作。但是,如果没有这个,我就无法工作。在C ++中不能很好地支持本机C数组 - 它们的语法非常可怕。你应该使用包装类。

template<typename T, int firstdim, int seconddim> class TwoDimensionalArray {
    T data[firstdim][seconddim];
public:
    T*& operator[](int index) {
        return data[index];
    }
    const T*& operator[](int index) const {
        return data[index];
    }
};
class Myclass {
public:
    typedef TwoDimensionalArray<int, 5, 5> arraytype;
private:
    arraytype myarray;
public:
    arraytype& get_array() {
        return myarray;
    }
};

int main(int argc, char **argv) {
    Myclass m;
    Myclass::arraytype& var = m.get_array();
    int& someint = var[0][0];
}

This code compiles just fine. You can get pre-written wrapper class inside Boost (boost::array) that supports the whole shebang.

这段代码编译得很好。你可以在支持整个shebang的Boost(boost :: array)中获得预先编写的包装类。

#6


-1  

Change your int's to int[][]'s or try using int[,] instead?

将你的int更改为int [] []或尝试使用int [,]而不是?

#7


-1  

int **Myclass::get_array() { 
 return (int**)myarray; 
}