[問題] template一問
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Visual C++ 2008
問題(Question):
TEnMap與STL的Map操作類似
請問一下第二個class CItemInfoPool::AddItemInfo 裡面為何
可以直接這樣寫 pNewItemInfo->SetItemCode(strItemCode);
她是怎麼解析到T為CItemInfoBase?
另外我如果把<typename T> 代成其他類型,像int,這樣是不是就error了?
//Source Code
class CItemInfoBase
{
public:
typedef TEnMap<CEnAString, CItemInfoBase*> ItemInfoMAP;
typedef ItemInfoMAP::iterator ItemInfoMAP_ITER;
typedef ItemInfoMAP::const_iterator ItemInfoMAP_CITER;
typedef ItemInfoMAP::reverse_iterator ItemInfoMAP_RITER;
typedef ItemInfoMAP::const_reverse_iterator ItemInfoMAP_CRITER;
typedef ItemInfoMAP::value_type ItemInfoMAP_VTYPE;
/* 中略 */
public:
CItemInfoBase& operator = (const CItemInfoBase& info)
{
m_strItemCode = info.m_strItemCode;
return *this;
}
public:
void SetItemCode(const CEnAString& strItemCode){m_strItemCode=strItemCode;}
const CEnAString& GetItemCode() const {return m_strItemCode;}
protected:
CEnAString m_strItemCode;
};
//-------------------------------------------------------------------------
class CItemInfoPool
{
public:
/* 中略 */
template <typename T>
T* AddItemInfo(const CEnAString& strItemCode, const T& ItemInfo)
{
CItemInfoBase::ItemInfoMAP_ITER found=m_mapItemInfos.find(strItemCode);
if (found != m_mapItemInfos.end())
return (T*)found->second;
T* pNewItemInfo = new T;
*pNewItemInfo = ItemInfo;
pNewItemInfo->SetItemCode(strItemCode);
m_mapItemInfos[strItemCode] = pNewItemInfo;
m_iNumOfItemInfos++;
return pNewItemInfo;
}
const CItemInfoBase::ItemInfoMAP& GetItemInfos() const{return m_mapItemInfos;}
protected:
CItemInfoBase::ItemInfoMAP m_mapItemInfos;
};
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.120.1.136
※ 編輯: gogohata 來自: 59.120.1.136 (11/10 17:53)
※ 編輯: gogohata 來自: 59.120.1.136 (11/10 17:54)
→
11/10 18:01, , 1F
11/10 18:01, 1F
→
11/10 18:02, , 2F
11/10 18:02, 2F
→
11/10 18:19, , 3F
11/10 18:19, 3F
推
11/10 18:30, , 4F
11/10 18:30, 4F
→
11/10 18:31, , 5F
11/10 18:31, 5F
→
11/10 18:32, , 6F
11/10 18:32, 6F
→
11/10 18:32, , 7F
11/10 18:32, 7F
→
11/10 18:33, , 8F
11/10 18:33, 8F
→
11/10 18:33, , 9F
11/10 18:33, 9F
→
11/10 18:34, , 10F
11/10 18:34, 10F
→
11/10 20:04, , 11F
11/10 20:04, 11F
→
11/10 21:51, , 12F
11/10 21:51, 12F
→
11/10 23:10, , 13F
11/10 23:10, 13F
推
11/11 01:36, , 14F
11/11 01:36, 14F
→
11/11 01:36, , 15F
11/11 01:36, 15F
→
11/11 11:12, , 16F
11/11 11:12, 16F
→
11/11 11:23, , 17F
11/11 11:23, 17F
→
11/11 11:24, , 18F
11/11 11:24, 18F
推
11/11 12:00, , 19F
11/11 12:00, 19F
→
11/11 12:00, , 20F
11/11 12:00, 20F
→
11/11 12:01, , 21F
11/11 12:01, 21F
→
11/11 12:23, , 22F
11/11 12:23, 22F
→
11/11 12:57, , 23F
11/11 12:57, 23F
→
11/11 12:58, , 24F
11/11 12:58, 24F
→
11/11 13:33, , 25F
11/11 13:33, 25F
推
11/11 14:23, , 26F
11/11 14:23, 26F
→
11/11 14:24, , 27F
11/11 14:24, 27F
→
11/11 14:24, , 28F
11/11 14:24, 28F
→
11/11 14:24, , 29F
11/11 14:24, 29F
→
11/11 14:25, , 30F
11/11 14:25, 30F
推
11/11 15:04, , 31F
11/11 15:04, 31F
→
11/11 15:04, , 32F
11/11 15:04, 32F
→
11/11 15:37, , 33F
11/11 15:37, 33F
→
11/11 15:37, , 34F
11/11 15:37, 34F
討論串 (同標題文章)