2017-10-21 76 views
1

我是新来的C++,并试图将一个C++程序翻译成python。我感到困惑的下面代码的语法:功能定义中的配对变量

void function(unsigned(*f)(unsigned st, unsigned hqid)) const{ 
    if(f(st,hiddenControlBitId)){ 
    // code here 
    } 
} 

任何人都可以指出哪些是通过传递unsigned(*f)(unsigned st, unsigned hqid)作为参数是什么意思?

BTW,我肯定是不一样通过一对std::pair<int,int> f作为参数,因为试图调用使用一对函数给出一个编译错误​​。

回答

3
unsigned(*f)(unsigned st, unsigned hqid) 

这里f是一个指针,指向thakes 2个参数的函数,无论是无符号类型的,并返回未无符号整数。 sthqid可以完全省略。

用法示例:

如果你有这样的功能,例如:

unsigned foo(unsigned a, unsigned b) 
{ 
    return a + b; 
} 

,那么你可以调用function这样的(我想这是因为const存在的一种方法):

obj.function(foo); 
0

这意味着返回另一个的两个(无符号)整数的函数。有tools来解释C的这种臭名昭着的语法。

+0

我确信它不是这种情况,因为我试图用一个对来调用函数,这只会引发一个编译错误。但非常感谢那个网站! – taper

+1

@taper他的答案可能没有最好的措辞,但他是对的。 – bolov

+1

@taper它是单独传递的两个不同的参数,而不是作为单个“对” –

1

function是一个类的方法constconst不能应用于非CL屁股功能),它将一个参数f作为输入。

f是一个指向非类函数的指针,它以2 unsigned参数作为输入并返回unsigned

例如:

unsigned addThem(unsigned st, unsigned hqid) 
{ 
    return st + hqid; 
} 

someObj.function(&addThem); 
1
void function(unsigned(*f)(unsigned st, unsigned hqid)) const{ 
    if(f(st,hiddenControlBitId)){ 
    // code here 
    } 
} 

在此,function()是一些类接受单个参数,它是一个指向函数(名为f)的成员函数,它接受类型为unsigned的两个参数,并返回unsigned

如果foo()是定义为的函数;

unsigned foo(unsigned x, unsigned y) 
{ 
     return x + y; // some arbitrary operation on the arguments 
} 

并且如果s是具有function()作为成员函数,然后像一语句的类的一个实例;

s.function(foo); 

会调用函数foo()function()规范中的const意味着对象s不在逻辑上更改。