简体   繁体   中英

2D Array of pointers to objects

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

使用std :: array:

std::array< std::array< Mushroom*, 10>, 10>

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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