Search code examples
c++pass-by-reference

Is there any way force to pass an argument as reference?


As the question above, I want to make the code below working.

Several types of string arguments should be passed to a function.

    // declaration, maybe in .h file
    int matches(std::string& s1, std::string& s2, std::string& s3);

    inline int matches(std::string s1, std::string s2, std::string s3) {
        return matches((std::string&) s1, (std::string&) s2, (std::string&) s3); // here i can't figure out
    }


    // implementation
    int matches(std::string& s1, std::string& s2, std::string& s3) {
        ...
    }

    int main() {
        std::string foo("foo"), bar("bar"), baz("baz");

        matches("foo",  "bar",   "baz" );
        matches( foo,    bar,    "baz" );
        matches( foo,   "bar",   "baz" );
        ...
        matches("foo",   bar,    "baz" );
        matches( foo,    bar,     baz  );

        return 0;
    }

I've googled minutes, but couldn't find solution.

Do I need to give up this problem?

Any suggestion or reply would be appreciated.


Solution

  • Thanks to @RuslanTushov, I've found solution.

    I post my own answer here for anyone with the same problem.

        // implementation
        int matches(const std::string& s1, const std::string& s2, const std::string& s3) {
            ...
        }
    
        int main() {
            std::string foo("foo"), bar("bar"), baz("baz");
    
            matches("foo",  "bar",   "baz" );
            matches( foo,    bar,    "baz" );
            matches( foo,   "bar",   "baz" );
            ...
            matches("foo",   bar,    "baz" );
            matches( foo,    bar,     baz  );
    
            return 0;
        }