爲什麼從const char*
到std::string
的隱式轉換在後一種情況下不起作用? 請儘可能鏈接參考C++標準。數組初始值設定項列表中的隱式轉換
變型1:
struct Foo {
Foo(const char* a) {}
};
int main() {
// works well for a "const char*" accepting constructor
Foo* foo = new Foo[1] { "a" };
}
變體2:
struct Foo {
Foo(std::string a) {}
};
int main() {
// could not convert from "const char*" to "Foo"
Foo* foo = new Foo[1] { "a" };
}
有沒有這樣的事情作爲隱式演員。有一個cast(顯式類型轉換)和type coalescing(隱式類型轉換),決定哪一個。 – 2013-04-12 10:21:02
檢查如何命名的頁面: http://en.cppreference.com/w/cpp/language/implicit_cast – Hertz