in this code:

    void AdptSendReply(const char* str)
    string s(strlen(str) + 2);
    s = str;

void AdptSendReply(const string& str)
    string s(str.length() + 2);
    AdptSendReply(s); // use the next one

void AdptSendReply(string& str)
    if (AdapterConfig::instance()->getBoolProperty(PAR_LINEFEED)) {
        str += "\r\n";
    else {
        str += "\r";

i think c++ use of overloading ability.but how it can recognize which of the two functions "void AdptSendReply(string& str)" or "void AdptSendReply(const string& str)" must use?!

  • 1
    You know the first and the second aren't same. Second one passes empty string. Also the first is possibly unnecessary, as const string reference can be initialized by "c-string" (const char *) - temporary object is created with implicit conversion.
    – KIIV
    Jun 25, 2019 at 12:27
  • this may answer your question arduino.stackexchange.com/questions/3079/… ......... the compiler tests the argument of the function in a similar way
    – jsotola
    Jun 25, 2019 at 17:03
  • This is offtopic here you should ask it elsewhere, the main stackoverflow maybe? But yes this is overloading, and the compiler has rules to resolve which function to use; you can check the C++ manual, or a C++ Primer/tutorial for more information.
    – esoterik
    Jul 1, 2019 at 23:10
  • What is string? Why is this information missing from your question? Jul 2, 2019 at 0:25
  • It's probably std::string judging by the code, however this isn't really an Arduino question but a C++ question which might be better asked at Stack Overflow. I see people are voting to close this question, and if it gets closed just ask at Stack Overflow. They are experts on C++. :)
    – Nick Gammon
    Jul 2, 2019 at 5:24

2 Answers 2


Calling overloading function triggers a process called overload resolution, which uses argument types to select the proper function to call

  • If you supply an lvalue argument of type string, the compiler will call void AdptSendReply(string& str) version.

    string s = "hello";
    AdptSendReply(s); // calls 'string& str' version
  • If you supply an lvalue argument of type const string, the compiler will call void AdptSendReply(const string& str) version.

    const string s = "hello";
    AdptSendReply(s); // calls 'const string& str' version
  • If you supply an rvalue argument, the compiler will call void AdptSendReply(const string& str) version.

    AdptSendReply(string("hello")); // calls 'const string& str' version

And so on. The full set of rules that govern overload resolution is quite extensive, especially when the context involves implicit conversions.

It is not clear what you mean by "must use". Normally, overloaded functions will do the same thing (functionality-wise), meaning that you are not supposed to worry about which function to use. Just call it with the argument you have available and the compiler will select the proper function to call.

Also, what is string? How are we supposed to know that? Without knowing it, your question make little sense.


The compiler decides which function to use based on the variable type information. Imagine you write

str += "test";

If you declare str as a constant, the above line will throw an error. So if you write AdptSendReply(str) instead, the compiler will call the const function. Likewise, if you declare str as a mutable variable, the above line will compile fine, and, should you call AdptSendReply(str), the compiler will use the non-const function.

Not the answer you're looking for? Browse other questions tagged or ask your own question.