gpt4 book ai didi

c++ - C++中运算符的函数指针

转载 作者:可可西里 更新时间:2023-11-01 17:56:57 27 4
gpt4 key购买 nike

所以我有一个函数需要一个函数指针作为输入,它的原型(prototype)是这样的:

int function(int (*op)(int, int));

我想知道是否可以为该函数指针传递一个运算符。特别是这个内置运算符:

int operator|(int, int);

我已经试过了:

function(&(operator|));

得到了这样的错误:

error: no matching function for call to 'function(<unresolved overloaded function type>)'

我已经试过了:

function(&(operator|(int, int)));

得到了这样的错误:

error: expected primary-expression before 'int'

我在文档中查找过这种情况,但我得到的是关于“地址”运算符 (operator&) 的信息,而不是关于运算符地址的信息....

编辑:

见上一个问题 Calling primitive operator-functions explicitly in C++

最佳答案

内置运算符 |需要两个 int 并返回一个 int 不能通过符号 operator| 访问。例如,以下不编译

int a = operator|(2,3);

关于c++ - C++中运算符的函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17299933/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com