我有類變量聲明爲編譯時已知大小不變:如何在C++中將指針表示的二維數組轉換回二維數組?
static const int array[5][5]; // constants initlialised in another place
而且,幾乎返回它的函數:
virtual const int** getArray() { return array; }
如何獲得這個數組用這種方法,並將其轉換爲固定大小的數組,而不是基於指針的,所以我可以使用它像cout << data[2][2]
?
樣品是不到風度編譯:
const int[5][5] data = object->getArray();
cout << data[2][2];
樣品,編譯但崩潰的應用程序:
const int** data = object->getArray();
cout << data[2][2];
注:一個解決方案是創建typedef arr[5]
與arr*
聲明的方法,但我不我想創建一個typedef爲每個編譯時的大小,我使用像typedef arr5[5]; typedef arr10[10]
等。我正在尋找更類似於:
const int(*)[5] data = object->getArray(); // won't compile, example only
我們假設編譯時間常量數組裝載有動態的DLL和已在內存中,是否有可能使用此數據作爲數組不分配新的內存和編譯時間常數填充它?
就像我在上一個問題,說'const int的**'是不一樣的指針與二維數組。它不這樣工作。你不能施放它。如果需要,你可以將'const int *'強制轉換爲'const arr *',其中'arr'是一個typedef。 – 2013-05-07 08:44:24
'getArray'函數沒有意義,並且不是有效的C++。 – 2013-05-07 08:44:37