-
May 1st, 2015, 11:50 PM
#1
How to return a multidimensional array?
I have put aside C++ for a while. And I am back to it.
I forgot how to return a 2 dimensional arrays from a method?
Code:
Node** GetNodeMap() const { return m_Nodes; }
private:
Node m_Nodes[60][60];
How can I achieve that?
Thanks
Jack
-
May 2nd, 2015, 01:53 AM
#2
Re: How to return a multidimensional array?
Originally Posted by lucky6969b
Code:
Node** GetNodeMap() const { return m_Nodes; }
private:
Node m_Nodes[60][60];
Doesn't this code work?
Victor Nijegorodov
-
May 2nd, 2015, 02:03 AM
#3
Re: How to return a multidimensional array?
Error: return value type doesn't match the function type
I don't get an error code though.
The m_Nodes is underlined. And also I have left out the const identifier as well.
Thanks
Jack
-
May 2nd, 2015, 02:21 AM
#4
Re: How to return a multidimensional array?
Originally Posted by lucky6969b
I don't get an error code though.
If you plan to return a pointer to the 2D array you are asking for trouble. Chances are it gets stale and there will be a much harder to find runtime bug.
The most immediate solution is to return a copy of the array.
Another solution is to place the array on the heap and access it by way of a smart pointer. Then you would return a smart pointer.
Last edited by razzle; May 2nd, 2015 at 02:41 AM.
-
May 2nd, 2015, 03:19 AM
#5
Re: How to return a multidimensional array?
If I return a copy of the array, this also didn't compile
Code:
Node[60][60] GetNodeMap() { return m_Nodes; }
Nor this
Code:
Node[][] GetNodeMap() { return m_Nodes; }
Oh. I've just completely forgotten the syntax, please kindly help.
Thanks
Jack
-
May 2nd, 2015, 04:55 AM
#6
Re: How to return a multidimensional array?
In c++ functions can't return an array by value. You can use the array container in c++11 as in this example
Code:
class mymap {
private:
struct Node{
int data;
};
public:
typedef array < array<Node, 60>, 60 > ARRTYPE;
ARRTYPE GetNodeMap() const
{
return m_Nodes;
}
private:
ARRTYPE m_Nodes;
};
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|