C++类范围

Ada*_*all 3 c++ class

我从Objective C开始使用C++并遇到问题......这样可行: -

function1(char *filePath) {

    Box box(filePath); // construct/create a box using filePath

    // can use box in this function and destructor is called when function exits

}
Run Code Online (Sandbox Code Playgroud)

但是我需要这样的东西,其中function1和function2是异步调用的.

Box *boxPool[25]; // a pool of 25 box pointers

function1(int item, char *filePath) {

    boxPool[item](filePath); // construct/create a box, store a pointer in boxPool that is retained on exit

}

function2(int item) {

    // use the box from boxPool[item] and then destruct/release it on exit

}
Run Code Online (Sandbox Code Playgroud)

nos*_*nos 7

也许:

void function1(int item, char *filePath) {

    boxPool[item] = new Box(filePath);

}

void function2(int item) {

  //use boxPool[item]

  delete boxPool[item];
  boxPool[item] = NULL;
}
Run Code Online (Sandbox Code Playgroud)