c++中枚舉常量與宏常量的一點(diǎn)區(qū)別

字號(hào):


    #include<iostream>
    using namespace std;
    class A
    {
    enum { APPLE = 111 };
    #define PEAR 333
    public:
    A()
    {
    cout << "APPLE: " << APPLE << endl; // 私有枚舉常量,只有在類內(nèi)部可以訪問(wèn)。
    }
    };
    int main()
    {
    A a;
    //cout << "APPLE: " << APPLE << endl; // 編譯出錯(cuò),超出了訪問(wèn)范圍
    cout << "PEAR: " << PEAR << endl; // 正常執(zhí)行,宏替換,沒(méi)有范圍限制。
    return 0;
    }