I have code that works as expected:
EscapedString es("Abc&def");
EscapedString es2("");
es2 = es; // es2 == Abc%26def
And code that doesn't work as expected:
EscapedString es("Abc&def");
EscapedString es2 = es; // es == Abc%2526def
In the second case, the CTOR2 is called instead of CTOR3 even though es
is an EscapedString.
EscapedString es(EscapedString("Abc?def"));
Does the right thing, but I can't seem to set a breakpoint on CTOR3 so I'm not sure it is working correctly, or the code has been optimized away or it is accidentally working.
The class is below:
class EscapedString : public std::string {
public:
EscapedString(const char *szUnEscaped) { // CTOR1
*this = szUnEscaped;
}
EscapedString(const std::string &strUnEscaped) { // CTOR2
*this = strUnEscaped;
}
explicit EscapedString(const EscapedString &strEscaped) { // CTOR3
*this = strEscaped; // Can't set breakpoint here
}
EscapedString &operator=(const std::string &strUnEscaped) {
char *szEscaped = curl_easy_escape(NULL, strUnEscaped.c_str(), strUnEscaped.length());
this->assign(szEscaped);
curl_free(szEscaped);
return *this;
}
EscapedString &operator=(const char *szUnEscaped) {
char *szEscaped = curl_easy_escape(NULL, szUnEscaped, strlen(szUnEscaped));
this->assign(szEscaped);
curl_free(szEscaped);
return *this;
}
EscapedString &operator=(const EscapedString &strEscaped) {
// Don't re-escape the escaped value
this->assign(static_cast<const std::string &>(strEscaped));
return *this;
}
};