当前位置 : 主页 > 编程语言 > c++ >

C类范围

来源:互联网 收集:自由互联 发布时间:2021-06-23
我是从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} 但是我需
我是从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

}

但是我需要这样的东西,其中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

}
也许:

void function1(int item, char *filePath) {

    boxPool[item] = new Box(filePath);

}

void function2(int item) {

  //use boxPool[item]

  delete boxPool[item];
  boxPool[item] = NULL;
}
网友评论