void Frame::ExtractORB(int flag, const cv::Mat &im)
{
if(flag==0)
(*mpORBextractorLeft)(im,cv::Mat(),mvKeys,mDescriptors);
else
(*mpORBextractorRight)(im,cv::Mat(),mvKeysRight,mDescriptorsRight);
}
这样的格式怎么用? (*名称)(参数 1、参数 2、参数 3、参数 4)
此代码属于 orb_slam .... https://github.com/raulmur/ORB_SLAM2
最佳答案
指向非成员函数的指针的声明具有以下结构:
返回类型(*名称)(参数列表)
因此,为了声明一个指针 func
指向一个以 const char*
和 int
作为参数并返回 的函数>bool
你说
bool (*func)(const char*, int);
请注意,您需要将 *func
括起来,否则编译器会将其解释为函数声明
bool* func(const char*, int);
返回 bool*
。
现在假设你有一个函数
bool foo(const char* x, int y) { /* ... */ }
相应的类型,那么你可以让你的指针指向这个函数
func = &foo;
为了调用func
指向的函数,您取消引用指针并使用这样的参数调用它
bool result = (*func)("abc", 3);
这就是您在提供的示例中所做的。使用给定的参数调用 mpORBextractorLeft
(resp. mpORBextractorRight
)指向的函数。
一些进一步的说明:
可以省略函数指针的解引用,所以
bool result = func("abc", 3);
应该也能正常工作。
声明指针时,可以立即初始化:
bool (*func)(const char*, int) = &foo;
如果您需要多次写入指针的类型,可以使用
typedef
来简化代码:typedef bool (*fun_ptr)(const char*, int); fun_ptr func1 = &foo; fun_ptr func2 = &bar;
关于c++ - 如何使用 (*mpORBextractorLeft)(in,cv::Mat(),monKeys,Descriptors);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50871290/