[問題] 記憶體物件初始化

看板C_and_CPP作者 (The Beginning)時間15年前 (2010/12/24 15:53), 編輯推噓3(305)
留言8則, 5人參與, 最新討論串1/1
現在有一個指標 void* ptr = address; 想要在 ptr 所指的這個地方, new 一個新的物件, 請問該怎麼 new 呢? ptr = new xxxOBJ(); //這個寫法好像是把 分配到的位址給 ptr -- The Beginning is the end The End is the beginning -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.87.64.222 ※ 編輯: powertodream 來自: 219.87.64.222 (12/24 15:55)

12/24 16:10, , 1F
你的意思是要在指定位置建一個物件?
12/24 16:10, 1F

12/24 16:16, , 2F
cherryfog: 是的
12/24 16:16, 2F

12/24 16:26, , 3F
關鍵字: placement new
12/24 16:26, 3F

12/24 21:49, , 4F
std::allocator
12/24 21:49, 4F

12/25 22:30, , 5F
ya, placement new, 跟 std::allocator 沒關係 @@
12/25 22:30, 5F

12/26 00:37, , 6F
用allocator::construct可以用copy ctor建構物件, 用
12/26 00:37, 6F

12/26 00:38, , 7F
allocator還有他的typedefs可以避免在code中嵌入太多
12/26 00:38, 7F

12/26 00:39, , 8F
跟型別相關的細節
12/26 00:39, 8F
文章代碼(AID): #1D555aRu (C_and_CPP)