C ++类范围

时间:2010-01-29 15:37:22

标签: 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

}

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

}

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

Box *boxPool[25];

boxPool是一个包含25个指向Box对象的数组。

Box boxPool[25];

是一个包含25个Box对象的数组。