[問題] 動態宣告class的array並使用constructor

看板C_and_CPP作者 (J.D.)時間12年前 (2013/05/28 03:26), 編輯推噓2(205)
留言7則, 5人參與, 最新討論串1/3 (看更多)
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Linux 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) none 問題(Question): 我想要動態宣告object的array 該array 共n個object 然後每一個object的constructor再動態宣告一個int array 該array 共m個int class myclass { public: int* array; myclass(int n); }; myclass::myclass(int n) { array = new int[n]; } int main() { int n = 7; int m = 13; myclass *x; //打 x = new myclass[n](m); 不對 //打 x = new myclass(m)[n]; 也不對 } 請問要怎麼做? 不想用vector... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.246.179

05/28 11:34, , 1F
http://codepad.org/EBCekS6m 不知道是不是你想要的?
05/28 11:34, 1F

05/28 12:07, , 2F

05/28 12:19, , 3F
樓上跟原 po 原義相近, 不過 placement new 之前最好手動
05/28 12:19, 3F

05/28 12:19, , 4F
destruct 吧. Before line 19: array[i].~A();
05/28 12:19, 4F

05/28 15:16, , 5F
有什麼原因讓你不想用vector?
05/28 15:16, 5F
用多少 取多少~ 當我所要用到的只是 單純的array 沒用到其他東西 我會希望是用array 而不是vector 因為我所要實作的程式 大多是要跑大測資 要比速度的 之前我有一份程式 就是覺得跑很慢 我把所有vector改成 int* 速度就真的快了有10幾秒以上 ※ 編輯: musicJD 來自: 111.255.249.95 (05/29 01:06)

05/29 01:34, , 6F
如果你 allocator 不是自己寫的當然有差
05/29 01:34, 6F

05/29 01:51, , 7F
2樓 ctor/dtor 呼叫次數不對稱
05/29 01:51, 7F
文章代碼(AID): #1Hf2FDAa (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1Hf2FDAa (C_and_CPP)