0

I'm trying to have a manager class that contains an array of array of pointers to certain objects, let's call these objects mushrooms.

But I have no idea how the syntax for the decleration should go and how to access the pointers once in another function once I have it declated. Here are some ways that I thought the declarations should go..

Mushroom** mushroomArray;

Mushroom* mushroomArray[10][10];

Mushroom mushroomArray[10][10];

Are any of these valid? What are the differences?

And how would I go about accessing the pointers to the mushrooms in a function after the 2D array has been declared "correctly"?

Thanks

4
  • We really need a multidimensional std::array. Commented Apr 23, 2014 at 21:23
  • The first one is easy to discard because it is not an array. Then you're left with two, only one of which involves pointers. Commented Apr 23, 2014 at 21:23
  • 1
    why don't you use a vector of vectors of pointers? std::vector<std::vector<Mushroom*>> or if size is known a std::array of std::arrays of pointers. Commented Apr 23, 2014 at 21:25
  • Don't use raw pointers. That way lies memory leaks and double frees. The standard library contains a bunch of useful containers which have been thoroughly tested and optimized. Only after you've mastered the standard library containers and know that none of them suffice should you roll your own structures with raw pointers. Commented Apr 23, 2014 at 21:36

2 Answers 2

2

Use std::array:

std::array< std::array< Mushroom*, 10>, 10>
Sign up to request clarification or add additional context in comments.

Comments

1

an array of array of pointers

This should be pretty straight-forward:

Mushroom* mushroomArray[10][10];

And how would I go about accessing the pointers to the mushrooms in a function

Are you talking about a local variable that is defined and used inside the same function?

void someFunction()
{
    Mushroom* mushroomArray[10][10];
    mushroomArray[0][0] = new Mushroom("your", "arguments", "here");
}

Or are you talking about defining an array in one function and then using it inside another function?

void someFunction(Mushroom* (*p)[10])
{
    p[0][0] = new Mushroom("your", "arguments", "here");
}

int main()
{
    Mushroom* mushroomArray[10][10];
    someFunction(mushroomArray);
}

2 Comments

The function belongs to the class that holds the 2D Array, so the function should have access to it. Inside the function I wrote a double nested for loop to Spawn mushrooms around a scene in a gridlike fashion, and to have the mushrooms stored as pointers for the 2D Array. But I'm having trouble with the storing part.
Please update your question with the code, it is not very readable inside comments. But that new Mushroom surely looks suspicious, because you're not doing anything with the resulting pointer of new.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.