看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《developers (alpine skier)》之銘言: : template<class T> : Base* Get() : { : return new T; : } : template<> : Base* Get<D1>() : { : return new D1; : } : template<> : Base* Get<D2>() : { : return new D2; : } : template<> : Base* Get<D3>() : { : return new D3; : } 請問template中如果使用 new 時,讓此 template 變成dll後 會不會發生 new, delete 使用的 c++ runtime 版本不合的問題呢? a.cpp: Base* a_instance=Get<Type1>(); //使用a.cpp 中的runtime去 new Release(a_instance); //使用template library dll中的runtime 去delete //new, delete 使用的版本不同,可能發生問題? thanks~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.193.31.42
developers:這個問題似乎變成在不同的dll中去new和delete同一個 06/19 00:23
developers:物件,應該是會有問題的。原本的例子只是想說明可以用 06/19 00:25
developers:template specialization 或 traits,依據不同的type 06/19 00:26
developers:來做選擇;在template function 裡用new並return一個 06/19 00:27
developers:raw pointer的確不是一個好的design 06/19 00:28
bignose1007:如果template裡頭想用new要怎麼處理呢? 06/19 00:47