lundi 1 février 2016

How to Hide STL Functor Structs in C++ Without Granting Private Member Access

I have a class, which stores items in an std::unordered_map. The key for the map is a custom type, so I have a helper struct defined as follows:

struct hash_equ {
    // Hasher
    size_t operator()(myType const &val) const;

    // Equality comparer
    bool operator()(myType const &a, myType const &b) const;
};

Then, within the class declaration, I have the following:

class Foo {
private:
    typedef std::unordered_map<myType const &, int, hash_equ, hash_equ> map_type;

    map_type _myMap;

    ...
};

The problem is that I want to hide the existence of hash_equ from code outside Foo.cpp, but hash_equ must be defined in Foo.h in order to declare the member Foo::_myMap. The only idea I could come up with is to declare hash_equ as a private, inner class of Foo. However, in C++11, doing so would grant access to Foo private members to hash_equ. Is there some way to hide hash_equ from the client while also preventing access to Foo's private members?

Aucun commentaire:

Enregistrer un commentaire