如果我想要一個指向數組的指針數組,並且數組的大小從一開始就不知道該怎麼辦?我只是好奇,如果有辦法做到這一點。使用新的陳述或其他東西。事情尋找類似如何動態創建函數數組?
void (* testArray[5])(void *) = new void()(void *);
如果我想要一個指向數組的指針數組,並且數組的大小從一開始就不知道該怎麼辦?我只是好奇,如果有辦法做到這一點。使用新的陳述或其他東西。事情尋找類似如何動態創建函數數組?
void (* testArray[5])(void *) = new void()(void *);
你可以使用一個std::vector
:
#include <vector>
typedef void (*FunPointer)(void *);
std::vector<FunPointer> pointers;
如果你真的想用一個靜態數組,這將是更好的使用我的片段上面定義的FunPointer
它做:
FunPointer testArray[5];
testArray[0] = some_fun_pointer;
儘管我仍然會選擇矢量解決方案,但考慮到在編譯時和編譯期間您不知道數組的大小T優使用C++,而不是C.
for(i=0;i<length;i++)
A[i]=new node
或
#include <vector>
std::vector<someObj*> x;
x.resize(someSize);
隨着typedef
,新表達是微不足道:
typedef void(*F)(void*);
int main() {
F *testArray = new F[5];
if(testArray[0]) testArray[0](0);
}
沒有typedef
,則較爲困難:
void x(void*) {}
int main() {
void (*(*testArray))(void*) = new (void(*[5])(void*));
testArray[3] = x;
if(testArray[3]) testArray[3](0);
}