BOOL與bool的區別(bool不是c的關鍵字,c++中bool也不是int)

來源:互聯網
上載者:User
  1. BOOL 定義 
    typedef int BOOL; 
    #define FALSE 0 
    #define TRUE 1 
    不能完全重載(BOOL的本質就是int),如: 
    //file.h void f(int ); // ok 
    void f(BOOL); // ok // 
    file.cpp 
    void f(int) {/*….*/} // ok 
    void f(BOOL) {/*….*/} // 錯誤,重新聲明
  2. bool(false,true)是C++內建的類型,可以避免上述的問題。

bool在C++裡是佔用1位元組,而BOOL是int類型,int類型的大小是視具體環境而定的;所以來說:false/true只佔用1個位元組,而 TRUE/FALSE視具體環境而言.

以下是BOOL在windef.h中的定義:
typedef int BOOL;
false/true是標準C++語言裡新增的關鍵字,而FALSE/TRUE是通過#define,用途是解決程式在C與C++中環境的差異,以下是FALSE/TRUE在windef.h的定義: 
    #ifndef FALSE
    #define FALSE 0 
    #endif
    #ifndef TRUE
    #define TRUE 1
    #endif 
也就是說FALSE/TRUE是int類型,而false/true是bool類型;所以兩者不一樣的,只不過我們在使用中沒有這種感覺,因為C++會幫你做隱式轉換。

linux 中c++ 編譯器要用g++,否則編譯器不認識bool類型。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.