c++ - 虚拟 int 运算符()(int k);

标签 c++ visual-c++

在标题中:

#include <iostream>
#include <vector>
using namespace std;

template<class Key>
class HashFunction{
public:
    int N;
    virtual int operator()(Key k)=0;
};

class MyHashFunction : public HashFunction <int> {
public:
    virtual int operator()(int k);

};

然后在cpp文件中:

#include "Hash classes.h"

int MyHashFunction::operator ()(int k){
    return k% this->N ;
}

谁能解释一下这个语法: virtual int operator()(Key k)=0; 我了解虚拟方法和'=0'以及模板是什么......一般来说,但我无法弄清楚这个“int operator()(Key k)”的含义以及它在cpp文件中的使用方式,我仍然没有太多在 C++ 中使用这些概念的经验,所以语法很烦人

非常感谢您的宝贵时间。

最佳答案

您为对象 MyHashFunction 定义了 operator(),这意味着您可以像调用一个函数一样调用一个实例。

例如

MyHashFunction myHashFunctionInstance;
myHashFunctionInstance(20); //Call the operator()

关于c++ - 虚拟 int 运算符()(int k);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20165134/

相关文章:

c++ - 我如何找出编译器花费时间的地方?

c++ - 在 Struct 或 Union 上使用 ntohl

c++ - 所有类的 pretty-print ,支持基于范围的 for 循环

c++ - 如何知道对象的所有者类

windows - 检测 C/C++ 中字符串的编码

c++ - 错误 C2660 : 'std::allocator<char>::allocate' : function does not take 2 arguments

c - 为什么会出现访问冲突运行时错误?

c++ - 使用#import添加msado28.tlb库时,在不相关的文件中引起的问题

c++ - 在 C 的 main 中调用函数后定义和声明函数是否可移植?

c++ - 加快一些 SSE2 Intrinsics 的颜色转换