0

Is it legal to define two class functions, one overloaded with a reference to a string and the other overloaded with a const char*?

void funcA(const std::string& s)
void funcA(const char* s)

Can I guarantee that if I call funcA() with a const char* input, it will not call the string function? I'm asking because there is an implicit construction from a const char* to a string.

1 Answer 1

4

Yes, it's valid. If you pass a const char*, the second overload is an exact match, which is preferred over all other overloads, particularly ones involving user-defined conversions (such as converting to a const std::string).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.