我有一个 20-30 行的 C++ 函数,它引用了一个 int
以进行可能的更新。现在,我将传递给它的成员替换为封装更多数据的成员成员,例如:
searchState.matchedOK = specificSearch(*pageIndices.GetStringIByRef(), searchState); //new, and overwritten.
searchState.matchedOK = specificSearch(pageOffsetInText, searchState); //old
我想将此修改本地化到上面调用的行和函数,因为一旦我验证了等价性和等价性,就应该删除旧成员。
这可以通过简单的转换实现吗?
如果你想要代码:
static bool specificSearch(int &matchLocation, const SearchSpecs &specs) {/**/}
和最近添加的成员:
inline unsigned int *GetStringIByRef() {return &stringI;}
最佳答案
我不是 100% 确定我理解你的问题,所以我可能完全错过这里的标记。但是,如果您将函数设为模板:
template<typename IntType>
static bool specificSearch(IntType &matchLocation, const SearchSpecs &specs) {/**/}
这将允许您将任一类型传递给它。
关于c++ - 如何通过引用或指针将 `int` 和 `unsigned int` 成员传递给同一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9795488/