2011-01-04 4 views
3

나는 error_code이라는 클래스가 있습니다. 나는 std::mapCMap (MFC)의 열쇠로 사용한다. std::map에 대해서는 작동 가능하지만, CMap에 대해서는 작동하지 않습니다. 내가 어떻게 그렇게 할 수 있는지 알 겠어?CMap의 키 객체

// OK! 
std::map<error_code, int> m; 
m[error_code(123)] = 888; 

// error C2440: 'type cast' : cannot convert from 'error_code' to 'DWORD_PTR' 
CMap <error_code, error_code&, int, int& > m; 
m[error_code(123)] = 888; 

class error_code { 
public: 
    error_code() : hi(0), lo(0) {} 
    error_code(unsigned __int64 lo) : hi(0), lo(lo) {} 
    error_code(unsigned __int64 hi, unsigned __int64 lo) : hi(hi), lo(lo) {} 

    error_code& operator|=(const error_code &e) { 
     this->hi |= e.hi; 
     this->lo |= e.lo; 
     return *this; 
    } 

    error_code& operator&=(const error_code &e) { 
     this->hi &= e.hi; 
     this->lo &= e.lo; 
     return *this; 
    } 

    bool operator==(const error_code& e) const { 
     return hi == e.hi && lo == e.lo; 
    } 

    bool operator!=(const error_code& e) const { 
     return hi != e.hi || lo != e.lo; 
    } 

    bool operator<(const error_code& e) const { 
     if (hi == e.hi) { 
      return lo < e.lo; 
     } 
     return hi < e.hi; 
    } 

    unsigned __int64 hi; 
    unsigned __int64 lo; 
}; 

답변

1

빠른 추적 템플릿 함수는 belows로 오류를 일으키는 것을 보여줍니다

template<class ARG_KEY> 
AFX_INLINE UINT AFXAPI HashKey(ARG_KEY key) 
{ 
    // default identity hash - works for most primitive values 
    return (DWORD)(((DWORD_PTR)key)>>4); 
} 

빠른 수정 프로그램은 암시 적 변환 기능을 추가 포함 할 것 사용자 정의 유형. 어떤 데이터가 저장 될지 잘 모르겠습니다. 따라서 임의의 속성을 선택하여 필요한 데이터를 구성하십시오.

class error_code { 

    ... 

    operator DWORD_PTR() const 
    { 
     return hi; 
    } 

    ... 
} 
+0

그러나 'hi'가 같지만 'lo'가 다른 경우 어떻게해야합니까? –

+0

또한 'hi'와 'lo'는 64 비트입니다. –

+0

오브젝트를 고유하게 식별하는 것을 선택하십시오. YeenFei는 그것을 언급하는 것을 잊었다. – RedX