如何在不授予私有成员访问权限的情况下隐藏 C++ 中的 STL 函子结构
How to Hide STL Functor Structs in C++ Without Granting Private Member Access
我有一个类,它将项目存储在 std::unordered_map 中。 映射的键是自定义类型,因此我有一个帮助程序结构,定义如下:
struct hash_equ {
// Hasher
size_t operator()(myType const &val) const;
// Equality comparer
bool operator()(myType const &a, myType const &b) const;
};
然后,在类声明中,我有以下内容:
class Foo {
private:
typedef std::unordered_map<myType const &, int, hash_equ, hash_equ> map_type;
map_type _myMap;
...
};
问题是我想从 Foo.cpp 外部的代码中隐藏hash_equ
的存在,但必须在 Foo.h 中定义hash_equ
才能声明成员Foo::_myMap
。 我能想到的唯一想法是将hash_equ
声明为一个私有的、内部的Foo
类。 但是,在 C++11 中,这样做将授予Foo
私有成员访问hash_equ
的权限。 有没有办法对客户端隐藏hash_equ
,同时阻止访问Foo
的私人成员?
为什么不使用私有的实现命名空间?
// This can be in either Foo.h or in a file included by it
namespace detail
{
struct hash_equ
...
}
然后像这样使用它:
typedef std::unordered_map<
myType const &,
int,
detail::hash_equ,
detail::hash_equ>
相关文章:
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- 结构和双指针隐藏在其他结构中,多层混淆
- 在 cpp 文件中隐藏采用模板参数引用的方法
- 参数包构造函数在类模板中隐藏用户定义的转换
- 如何等待窗口隐藏在Qt中?
- 如何使用类型别名从模板化类中隐藏模板列表
- 在设计 SDK 时,我是否应该在 C++ 头文件中完全隐藏内部类?
- 名称隐藏对静态函数继承的实例使用
- 在 Linux 中使用 ioctl() 获取隐藏功能报告时,零字节消失
- 从多个模板化基类派生时出现"隐藏重载的虚函数"警告
- 如何在 Omnet++ 5.6.1 中隐藏箭头?
- 在C++中隐藏键盘记录器的控制台窗口
- 从静态库使用时隐藏不透明结构的内容
- 隐藏用于聚合初始化的空基类
- 使用智能指针指向 C 库中的结构,该结构通过 typedef 隐藏实现(即不完整的类型)
- Qt中的包装连接方法隐藏了编译器所需的信息
- 隐藏重载虚拟功能的模板化访客:SFINAE 在使用?
- 在 ui 文件中隐藏 QTabWidget 中的单个选项卡窗格?
- C++ [错误] 声明'char '隐藏参数。什么意思?
- 隐藏由 WinAPI 调用创建的窗口