问题描述
我正在使用Visual C ++& MFC。现在我需要使用CMap,键是我自己定义的特殊结构CMyKey。由于结构很大,我打算使用CMyKey&作为ARG_KEY。因此,我使用CMap如下:
I am using Visual C++ & MFC. Now I need to use CMap and the key is a special structure CMyKey defined by myself. Since the structure is large, I plan to use CMyKey& as the ARG_KEY. Therefore, I use CMap as below:
typedef CMap< CMyKey,CMyKey&,DWORD,DWORD> CMyMap;
typedef CMap<CMyKey, CMyKey&, DWORD, DWORD> CMyMap;
现在我需要实现集合类助手,如
https://msdn.microsoft.com/en-us/library/7y37x7cs.aspx
Now I need to implement the collection class helper as described in https://msdn.microsoft.com/en-us/library/7y37x7cs.aspx
它说这个功能应该是这样的:
It said the function should be like this:
模板<类TYPE,类ARG_TYPE>
template<class TYPE, class ARG_TYPE>
BOOL AFXAPI CompareElements(
BOOL AFXAPI CompareElements(
const TYPE * pElement1,
const TYPE* pElement1,
const ARG_TYPE * pElement2
const ARG_TYPE* pElement2
);
所以我写了模板的专业化功能如下:
So I write the specialization of the template function as below:
模板< > BOOL AFXAPI CompareElements< CMyKey,CMyKey&> (const CMyKey * pElement1,
const CMyKey& * pElement2)
template< > BOOL AFXAPI CompareElements<CMyKey, CMyKey&> (const CMyKey* pElement1, const CMyKey&* pElement2)
{
...
}
但编译器表示指向引用(const CMyKey& *)的指针是非法的。我必须修改如下函数:
But the compiler said a pointer to a reference(const CMyKey&*) is illegal. I have to modify the function as below:
template< > BOOL AFXAPI CompareElements< CMyKey,CMyKey> (const CMyKey * pElement1,
const CMyKey * pElement2)
template< > BOOL AFXAPI CompareElements<CMyKey, CMyKey> (const CMyKey* pElement1, const CMyKey* pElement2)
{
...
}
现在汇编很好。但我不明白为什么。由于我已经将ARG_KEY定义为CMyKey&而不是CMyKey,为什么我应该使用CMyKey而不是CMykey&作为ARG_TYPE?
Now the compilation is fine. But I cannot understand why. Since I have already defined ARG_KEY as CMyKey&, not CMyKey, why I should use CMyKey instead of CMykey& as the ARG_TYPE?
谢谢
推荐答案
CompareElements函数使用的是类型,而不是数据传递给KEY参数的CMap函数的方式,在你的例子是参考。
The CompareElements function is working with the type, not the way that data is passed to CMap functions for KEY arguments, which in your example is by reference.
这篇关于如何编写集合类助手CompareElements?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!