梅花雀莊

何震邦的個人網站,通常寫電腦和橋牌相關的文章,未來可能會寫關於日本麻將的東西

在 C++98 模擬 enum class

使用巢狀類別 (nested class) 可以達到類似的效果。因為我們不需要這些類別的實例 (instance),所以只需要宣告 (declaration),不需要定義 (definition)。

struct Color
{
    class Red;
    class Green;
    class Blue;
};

template<typename ColorType>
struct hex;

template<>
struct hex<Color::Red>
{ enum { value = 0xff0000 }; };

template<>
struct hex<Color::Green>
{ enum { value = 0x00ff00 }; };

template<>
struct hex<Color::Blue>
{ enum { value = 0x0000ff }; };

作者:何震邦

我叫何震邦,畢業於臺北醫學大學醫學系。在漢字不宜的場合,我也叫 Chen-Pang He。我的專長是微積分和數值線性代數。詳細資訊請洽本站的《關於》頁面。