Re: [問題] template一問

看板C_and_CPP作者 (bignose)時間14年前 (2011/06/18 13:45), 編輯推噓1(105)
留言6則, 2人參與, 最新討論串5/9 (看更多)
※ 引述《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

06/19 00:23, , 1F
這個問題似乎變成在不同的dll中去new和delete同一個
06/19 00:23, 1F

06/19 00:25, , 2F
物件,應該是會有問題的。原本的例子只是想說明可以用
06/19 00:25, 2F

06/19 00:26, , 3F
template specialization 或 traits,依據不同的type
06/19 00:26, 3F

06/19 00:27, , 4F
來做選擇;在template function 裡用new並return一個
06/19 00:27, 4F

06/19 00:28, , 5F
raw pointer的確不是一個好的design
06/19 00:28, 5F

06/19 00:47, , 6F
如果template裡頭想用new要怎麼處理呢?
06/19 00:47, 6F
文章代碼(AID): #1D_Alj_3 (C_and_CPP)
討論串 (同標題文章)
本文引述了以下文章的的內容:
問題
1
5
完整討論串 (本文為第 5 之 9 篇):
問題
2
3
問題
5
34
問題
3
16
問題
3
7
問題
1
4
問題
1
2
問題
1
5
問題
0
18
文章代碼(AID): #1D_Alj_3 (C_and_CPP)