2012-04-16 70 views
0

我的程序需要很多ANSI < => UNICODE對話,所以我想到了創建多種類型的對象,它將轉換所有的東西比聯合功能和大量的新/刪除更容易。僞代碼:C++多目標對象的想法

class CWchar // char based 
{ 
public: 
    public static implicit operator wchar_t*(CWchar cw) 
    { 
     // converting cw.data to wchar_t 
     // up to U+FFFF conversion needed 
    } 
    public static implicit operator char*(CWchar cw) 
    { 
     return cw.data; 
    } 
    CWchar& CWchar::operator=(const char* c) 
    { 
     data = *c; 
     return *this; 
    } 
    CWchar& CWchar::operator=(const wchar_t* c) 
    { 
     //conversion to char* ... 
     return *this; 
    } 

    // add some smart pointers, garbage collector, and leave delete 

private: 
    char* data; 
} 

這真的值得編碼嗎?或者我應該考慮另一種解決方案嗎?也許有已經完成的項目?或者,也許我錯了,這個想法很糟糕?謝謝

+3

爲什麼你需要做這麼多的轉換?是否有可能只有一個連接Unicode端和非Unicode端的橋? – 2012-04-16 12:42:10

+0

我的應用程序是迷你即時消息,它接收多字節字符*,我需要將其轉換爲寬wchar_t *,例如希臘字母將格式化好。 – user1112008 2012-04-16 12:51:22

+0

@MikeKwan我忘了補充通知,你能否多說一些關於橋樑的想法? – user1112008 2012-04-16 13:23:29

回答

1

這聽起來很像codecvt。這允許您在Char *和多字節wchar_t *流之間進行轉換。這是標準庫的一部分。 Stroustrup的C++編程語言第三版有一個很好的附錄。