2013-10-27 2 views
0

Google 해 스맵 해시 맵 라이브러리로 작업하고 있습니다.클래스 public 멤버 함수를 템플릿 매개 변수로 전달하는 방법은 무엇입니까?

class my_hashmap_key_class { 

private: 
    unsigned char *pData; 
    int data_length; 

public: 
    // Constructors,Destructor,Getters & Setters 

    //equal comparison operator for this class 
    bool operator()(const hashmap_key_class &rObj1, const hashmap_key_class &rObj2) const; 

    //hashing operator for this class 
    size_t operator()(const hashmap_key_class &rObj) const; 

}; 

가 지금은 HashFcndense_hash_map에 같은 Key으로 EqualKeymy_hashmap_key_class::operator()(const hashmap_key_class &rObj1, const hashmap_key_class &rObj2)my_hashmap_key_class::operator()(const hashmap_key_class &rObj)my_hashmap_key_class을 전달하려는 :로

template <class Key, class T, 
      class HashFcn = std::tr1::hash<Key>, 
      class EqualKey = std::equal_to<Key>, 
      class Alloc = libc_allocator_with_realloc<std::pair<const Key, T> > > 
class dense_hash_map { 
..... 
typedef dense_hashtable<std::pair<const Key, T>, Key, HashFcn, SelectKey, 
         SetKey, EqualKey, Alloc> ht; 
..... 

}; 

지금은 정의 내 자신의 클래스 : 그리고 나는 다음과 같은 클래스 템플릿을 클래스를 매개 변수로 사용할 때 다음과 같이 주 함수에서 사용합니다.

main.cpp :

dense_hash_map<hashmap_key_class, int, ???????,???????> hmap; 

클래스 매개 변수 함수를 템플릿 매개 변수로 전달하는 적절한 방법은 무엇입니까 ??

내가 좋아하는 통과 시도 :

dense_hash_map<hashmap_key_class, int, hashmap_key_class::operator(const hashmap_key_class &rObj1, const hashmap_key_class &rObj2),hashmap_key_class::operator()(const hashmap_key_class &rObj)> hmap; 

그러나 운영자가 감지되지 않는 나는 컴파일 오류가 발생합니다. 내가 잘못하고있는 곳을 깨닫도록 도와주세요.

+1

나는 그 연산자를'정적 인 '것으로 생각한다. 또한 동등한 비교를 위해'operator =='를 사용하지 않으시겠습니까? (여전히 수동으로 정의해야합니다.) – leemes

+1

'static'연산자를 만듭니다. –

+2

또는 클래스가 자체 equality functor라는 사실을 다소 이상하게 여긴다. 그러나 전례가 없으면 단순히'my_hashmap_key_class'를 전달하면된다. 비교를 수행하기 위해 인스턴스가 생성되며 매개 변수 중 하나도 생성 된 비교 자 자체가 될 수 없습니다. 'HashFn' 매개 변수에 대해서도 마찬가지입니다. 나는 그것을 자주 보지 못하지만,이 코드를 정의한 것처럼 아무 것도하지 않아야합니다. 그리고 클래스는 기본 구성을 지원해야합니다 (필자는 가정합니다). – WhozCraig

답변

0

의견에서 설명한대로 operator==으로 동등성을 작성해야합니다. 또한이 연산자를 정적으로 만들거나 매개 변수 중 하나를 제거하십시오 ("this"포인터가 평등 테스트의 왼쪽 피연산자가됩니다). 그렇지 않으면 예상대로 작동하지 않습니다.

//equal comparison operator for this class 
bool operator==(const hashmap_key_class &rObj2) const; 

//hashing operator for this class 
size_t operator()() const; 

다음, 당신의 클래스는이 같은 클라이언트 코드에 대한 준비가되어 있습니다 :

my_hashmap_key_class a = ...; 
my_hashmap_key_class b = ...; 

a == b; // calls a.operator==(b), i.e. compares a and b for equality 
a();  // calls a.operator()(), i.e. computes the hash of a 

그런 다음, 기본 템플릿 매개 변수를 사용하여 잘해야한다.

+0

사실입니다 .....하지만 객체가 인스턴스화되거나 멤버 함수가 정의되는 방식에 관심이 없습니다 (Google의 스파 스 해시 코드는 클래스가 특정 형식이어야하기 때문에). 하지만 멤버 함수를 템플릿 매개 변수로 사용하고이를 사용하는 방법. – annunarcist

+0

"기본 템플릿 매개 변수를 사용하면 괜찮을 것입니다."알아 두어야 할 부분과이를 달성하는 방법 – annunarcist

+0

간단히 매개 변수를 생략하십시오.'dense_hash_map '해야합니다. 그것을 시험하지 마라. 이 문제가 있으면보고하십시오. – leemes

관련 문제