C++ - 函子 vs 内联函数 vs 运算符重载

C++ - functor vs inline function vs operator overload?

本文关键字:vs 函数 运算符 重载 函子 C++      更新时间:2023-10-16

基于各种堆栈溢出帖子,我将以下内容放在我不久前做的车牌识别程序中:

可能板.h:

(lines omitted)
std::string strChars;
///////////////////////////////////////////////////////////////////////////////////////////////
static bool sortDescendingByNumberOfChars(const PossiblePlate &ppLeft, const PossiblePlate &ppRight) {
    return(ppLeft.strChars.length() > ppRight.strChars.length());
}
(lines omitted)

主.cpp

(lines omitted)
// sort the vector of possible plates in descending order (most number of chars to least number of chars)
std::sort(vectorOfPossiblePlates.begin(), vectorOfPossiblePlates.end(), PossiblePlate::sortDescendingByNumberOfChars);
(lines omitted)

如果更多上下文会有所帮助,这里是存储库:

https://github.com/MicrocontrollersAndMore/OpenCV_3_License_Plate_Recognition_Cpp

这段代码非常适合根据任何成员变量对C++中的对象向量进行排序,自从在其他各种项目中以来,我已经多次重复使用它。

我的问题是,这是什么? 函子、内联函数、运算符重载还是其他完全不同的东西? 如何确定差异?

也许我不明白这个问题,但如果指的是 std::sort,我会说它是一个模板函数 ref

我的问题是,这是什么?函子、内联函数、运算符重载还是其他完全不同的东西?

它只是一个函数,被转换为指向函数的指针。函数指针可以在函数调用运算符中使用,因此它可以用作std::sort的比较器。