c++ - 如何通过引用或指针将 `int` 和 `unsigned int` 成员传递给同一个函数?

标签 c++ pointers reference casting unsigned-integer

我有一个 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/

相关文章:

delphi - Delphi简单数据类型等效

c++ - Cocos2d-x 3.2 EventListener 在子 Sprite 中不起作用

c - 字符串转换为int

c - 传递二维数组然后没有得到值

c++ - 当您将一个指针分配给另一个指针时会发生什么?

c++ - 将返回值存储在引用中性能最差吗?

c++ - ReadFile(m_hComm, &cadena[rebut]==0, 1, &recibiendo, NULL);

c++ - Python - 如何检查弱引用是否仍然可用

c++ 虚函数 vs 成员函数指针(性能比较)

c++ - 这是操纵 C++ 容器元素的正确方法吗?