开发者

C++ class scope

开发者 https://www.devze.com 2022-12-18 17:07 出处:网络
I\'m coming to C++ from Objective C and have a problem... This works :- function1(char *filePath) { Box box(filePath); // construct/create a box using filePath

I'm coming to C++ from Objective C and have a problem... This works :-

function1(char *filePath) {

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

    // can use box in this function and destruc开发者_如何学Pythontor is called when function exits

}

However I need something like this, where function1 and function2 are called asynchronously.

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

}


Maybe:

void function1(int item, char *filePath) {

    boxPool[item] = new Box(filePath);

}

void function2(int item) {

  //use boxPool[item]

  delete boxPool[item];
  boxPool[item] = NULL;
}


Box *boxPool[25];

boxPool is an array of 25 pointers to Box objects.

Box boxPool[25];

Is an array of 25 Box objects.

0

精彩评论

暂无评论...
验证码 换一张
取 消