繁体   English   中英

为什么要调用错误的ctor?

[英]Why is the wrong ctor being called?

我的代码按预期工作:

EscapedString es("Abc&def");
EscapedString es2("");
es2 = es;     // es2 == Abc%26def

并且代码不能按预期工作:

EscapedString es("Abc&def");
EscapedString es2 = es;    // es == Abc%2526def

在第二种情况下,即使es是EscapedString,也会调用CTOR2而不是CTOR3。

EscapedString es(EscapedString("Abc?def"));

是对的,但我似乎无法在CTOR3上设置断点,所以我不确定它是否正常工作,或者代码已被优化掉或者它是意外工作的。

课程如下:

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;
    }
};

通常, EscapedString es2 = es; 将调用复制构造函数,但是您明确告诉它不要通过使复制构造函数explicit

explicit EscapedString(const EscapedString &strEscaped)

标记为explicit的构造函数永远不能通过自动类型转换来调用。 它只能被明确地调用,你在这里做了:

EscapedString es(EscapedString("Abc?def"));

这是编译器遇到EscapedString es2 = es;时发生的情况EscapedString es2 = es;

首先,编译器会看到它是否可以使用复制构造函数并发现它不能,因为它被标记为explicit 所以它寻找另一个构造函数来调用。 由于EscapedString是从std::string派生的,因此编译器能够将esconst std::string&并调用:

EscapedString &operator=(const std::string &strUnEscaped)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM