在标题中:
#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/