2013-11-23 107 views
1

在标题:virtual int operator()(int k);

#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 ; 
} 

谁能解释此语法请: 虚拟INT运算符()(密钥k)= 0; 我理解虚拟方法和“= 0”和......什么模板一般,但我有麻烦搞清楚这是什么“整数运算符()(密钥K)”的意思,它是那么如何在使用CPP文件,我仍然没有太多的经验在C++中使用这些概念,所以语法很烦人

谢谢你的时间,非常感谢。

+1

的C此功能++称为 “操作符重载”。 'operator()'(操作符括号)在这种情况下。通过Google了解更多信息。 – Drop

回答

1

您定义运算符()为对象MyHashFunction,这意味着你可以调用一个实例,就好像它是一个函数。

例如

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

这只是重载的函数调用操作符的虚拟版本,即()操作符。

例如:

void foo(Base const & x) 
{ 
    int n = x(1, 2, 3); // calls int Base::operator()(int, int, int), 
          // which may be virtual and dispatched dynamically 
} 
相关问题