Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std:访问类的私有成员的哈希_C++_C++11_Stdmap_Stdset_Stdhash - Fatal编程技术网

C++ std:访问类的私有成员的哈希

C++ std:访问类的私有成员的哈希,c++,c++11,stdmap,stdset,stdhash,C++,C++11,Stdmap,Stdset,Stdhash,我想散列一个有两个私有成员的类,例如: 福安 foo.cpp Foo::Foo (std::string _a, std::string _b) { this->a = _a; this->b = _b; } bool Foo::operator== (const Foo& other) const { return this->a == other.a && this->b == other.b; } bool Foo

我想散列一个有两个私有成员的类,例如:

福安

foo.cpp

Foo::Foo (std::string _a, std::string _b) {
    this->a = _a;
    this->b = _b;
}

bool Foo::operator== (const Foo& other) const {
    return this->a == other.a && this->b == other.b;
}

bool Foo::operator!= (const Foo& other) const {
    return !operator==(other);
}

std::size_t std::hash<Foo>::operator()(Foo const& foo) const {
    std::string f = foo.a; // << This wont compile!
    return 1;
}
在C++中,当最终哈希函数没有访问FO的私有成员时,FoH的散列通常是如何进行的?< /P>
您可以在答案中加入boost或abseil。

您可以将std::hash声明为Foo的朋友:

福班{ 私人: std::字符串a; std::字符串b; 公众: Foostd::stringa,std::stringb; 布尔运算符==常量Foo和其他常量; 布尔运算符!=常量Foo和其他常量; std::尺寸操作员const Foo&const; friend-std::hash; };
您可以做的是将std::hash声明为Foo的朋友:

福班{ 私人: std::字符串a; std::字符串b; 公众: Foostd::stringa,std::stringb; 布尔运算符==常量Foo和其他常量; 布尔运算符!=常量Foo和其他常量; std::尺寸操作员const Foo&const; friend-std::hash; };
如果Foo有一个名为std::size\u t get\u hash const;的公共成员函数会怎么样。。。?您可以使用一个好友声明来说明您的类和std::hash专门化。这比添加另一个不必要的成员函数污染接口要好。如果Foo有一个名为std::size\u t get\u hash const;的公共成员函数会怎么样。。。?您可以使用一个好友声明来说明您的类和std::hash专门化。这比添加另一个不必要的成员函数污染接口要好。
Foo::Foo (std::string _a, std::string _b) {
    this->a = _a;
    this->b = _b;
}

bool Foo::operator== (const Foo& other) const {
    return this->a == other.a && this->b == other.b;
}

bool Foo::operator!= (const Foo& other) const {
    return !operator==(other);
}

std::size_t std::hash<Foo>::operator()(Foo const& foo) const {
    std::string f = foo.a; // << This wont compile!
    return 1;
}