Search code examples
c++pointersreferencecastingunsigned-integer

How can I pass both `int` and `unsigned int` members, by reference or pointer, to the same function?


I have a 20-30 line C++ function which was taking a reference to an int for possible updation. Now I am replacing the member which was passed to it with a member of a member encapsulating more data, something like:

searchState.matchedOK = specificSearch(*pageIndices.GetStringIByRef(), searchState); //new, and overwritten.
searchState.matchedOK = specificSearch(pageOffsetInText, searchState); //old

I want to localise this modification to the lines and function called above because the old member should be removed once I have verified equivalence and beyond.

Is this possible with simple casts?

If you want code:

static bool specificSearch(int &matchLocation, const SearchSpecs &specs) {/**/}

and the recently added member:

inline unsigned int *GetStringIByRef() {return &stringI;}

Solution

  • I'm not 100% sure I understand your question, so I might completely miss the mark here. But if you make your function a template:

    template<typename IntType>
    static bool specificSearch(IntType &matchLocation, const SearchSpecs &specs) {/**/}
    

    That will allow you to pass either type to it.