在内联函数中调用外部函数

Calling outside functions in inline functions

本文关键字:函数 调用 外部      更新时间:2023-10-16

假设我有内联函数:

// equality operator where left operand is a long
inline bool operator==(long num, BigInt const& val) {
    return compare(num, val) == 0;
}

其中"compare"是在BigInt.h中定义的,内联函数就在那里。我如何使用compare,甚至可以使用它?

BigInt.h

class BigInt {
public:
//code
int BigInt::compare(long num, BigInt const& other) const;
//code
};
// equality operator where left operand is a long
inline bool operator==(long num, BigInt const& val) {
    return compare(num, val) == 0;
}

compare是一个成员函数,您应该像一样更改对它的调用

// equality operator where left operand is a long
inline bool operator==(long num, BigInt const& val) {
    return val.compare(num, val) == 0;
}

我仍然怀疑为什么compare是一个成员函数。如果它与当前对象无关,那么它应该只是一个普通函数或静态成员函数。