c++ - C++中是否可以继承operator()?

标签 c++ visual-c++ inheritance hashmap

我正在为 hash_map 编写一些散列函数示例。如果我使用编译器定义的 hash_map,我需要在 Hasher 中定义 Comparer。我知道最好使用 tr1::unordered_map 但在我的应用程序中,重要的是将最小桶数设置得相当大并定义平均 bucket_size - 增长的条件。

所以我想在基类 Foo 中实现比较器,并在其他哈希器中继承它,例如 Bar。

class Foo
{
public:
    Foo(const SeedParam& dim);
    Foo(const Foo& src);
    Foo& operator = (const Foo& src);
    virtual bool operator ()(const Index2& ind1, const Index2& ind2) const;
    size_t operator() (const Index2& ind) const;

    enum
    {
        bucket_size = 4,
        min_buckets = 32768,
    };
protected:
    SeedParam _dim;
    const hash_compare<unsigned long long> _stdHasher;
};

class Bar: public Foo
{
public:
    Bar(const SeedParam& dim);
    size_t operator() (const Index2& ind) const;
};

但是编译器在 hash_map 中编译这样的代码时说“一个术语不计算为一个带两个参数的函数”:

if (!this->comp(this->_Kfn(*_Where), _Keyval))

那么是否可以继承 operator() 呢?我的类(class)有什么问题?

最佳答案

类是名称查找的范围,派生类(仍用于名称查找)嵌套在它们的基类中。

当搜索一个名称时(operator() 就是这样一个名称),搜索将在包含它的第一个作用域停止。它不会继续封闭作用域来寻找潜在的过载。

在这里,在 Bar 作用域中搜索 operator(),有一个,因此在 Foo 中找不到带有两个参数的重载。

解决方法是添加

using Foo::operator();

在酒吧里。

关于c++ - C++中是否可以继承operator()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2423816/

相关文章:

c++ - 如何使用 FLTK in-box.get_string()

c++ - 将 gui 添加到 c++ 项目 (visual studio 2010)

javascript - Javascript 中的继承 - 子方法调用父方法

c++ - 在C++中推导继承方法的父类

c++ - 尝试更改游戏状态时出现段错误

C++ 自动为 boost::make_shared 生成包装器

c++ - 使用类模板需要参数列表

c++ - 我如何让 Doxygen 扩展包含文件中的宏?

c++ - 静态库中带有 std::cout 的 MSVC 2010 链接器错误 2005

oop - 我可以在继承类上获取父类名称(第一个)吗?