轉換運算子
你可以過載型別運算子,以便可以將型別隱式轉換為指定的型別。
必須在 class / struct 中定義轉換運算子 :
operator T() const { /* return something */ }
注意:運算子是 const,允許轉換 const 物件。
例:
struct Text
{
    std::string text;
    // Now Text can be implicitly converted into a const char*
    /*explicit*/ operator const char*() const { return text.data(); }
    // ^^^^^^^
    // to disable implicit conversion
};
Text t;
t.text = "Hello world!";
//Ok
const char* copyoftext = t;