Latest Entries »

C++ Terminology – 1. Variety of Types

Fundamental Type

Aka. built-in type, primitive type, native type and etc. C++ core language 原生支援型別. 包含pointer, reference 以及用 const 或 volatile 修飾過的型別 (不包含 C++ Standard Library 定義的型別!), e.g.:

  • float
  • volatile unsigned int
  • const char*
  • double&

Integral Type

為 fundamental type 的 subset. 代表整數的 fundamental type, e.g.:

  • char
  • unsigned short
  • long

User-defined Type

Aka. class type. 使用者自訂型別, 也稱為類或類別. 泛指所有的 struct 與 class, 包含 C++ Standard Library 定義的型別, e.g.:

    class SomeClass;
    struct SomeStruct;
    template  class basic_string;

對 C++ 來說, 在定義 user-defined type 時, classstruct 幾乎是沒有差別且能互相替換 (interchangable). 唯一的差別是預設 (默認) 的存取權限:

    class DerivedClass : /*private*/ BaseClass
    {
    /*private:*/
      int data;
    };
    struct DerivedStruct : /*public*/ BaseStruct
    {
    /*public:*/
      int data;
    };

POD

Acronym for Plain Old Data. User-defined C-style structure. 為 user-defined type 的 subset, 需符合下列條件:

  1. 沒有 user-defined constructor, destructor 以及 assignment operator
  2. 沒有 base class type, 換句話說非 derive 自 user-defined type
  3. 如果有 data member, 其所有 data member 必須為 fundamental type 或 POD

下兩例皆為 POD:

    class SomePodType
    {
    public:
      unsigned int data0;
    };
    struct AnotherPodTypeWithDataMember
    {
      SomePodType data1;
      char data2;
    };

Template Class

也是 user-defined type 的 subset. 為 class template 的 instance (具現), e.g.:

    typedef list<char> IntList;

上例中 list; 為 class template, IntList 為 template class.

Type

泛指任何 fundamental type 或 user-defined type.

在 open source 的世界也好, 在公司內部也好, 任何像樣的 project, 大都免不了採用或自訂一套 coding convention. 不同 coding convention 所要達成的目標並不盡相同, 但都有個共同點 – 讓你寫的 code 看起來像是別人寫的. 主要原因是 – 這樣做應該可以讓他人容易閱讀/理解/修改你寫的 code.

Coding convention 主要分為兩個部份:

  • Naming notation (命名法)
  • Formatting (程式格式/排版)

以我個人經驗以及從朋友處得到的消息來看, 公司內部最常採用的 naming notation 是 Hungarian Notation (匈牙利命名法). 把 Hungarian notation 傳播到世界各地, 甚至錯誤地使用在例如 C++ 之類的 strong-typed (或是接近 strong-typed) 語言上, M$ 功不可沒, Win32 SDK 與 MFC 是其中經典之作. 時至今日, 就連 M$ 內部也已展開了反撲, 最好的例子就是 .NET 丟開 M$ 過去採用 hungarian notation 的包袱.

痛恨 hungarian notation, 但這通常不會造成我在工作上的困擾. 我總是能幸運地說服同事與上司放棄它, 或至少不強迫我採用它. 不過還是常會有朋友跟我抱怨公司強迫他們採用 hungarian notation, 他們多是無奈接受. 這帖 blog, 就是為了這些朋友寫的, 如果你剛好在這些公司工作, 不妨把這帖 blog 的 link 轉給你的同事或上司.

Annoyances of the Hungarian Notation:

  • 視你為弱智. 認為你替變數取的名字大都不好, 很難從名字辨認大約的型別
  • 視你為白痴. 認為你有可能會 – 把一個叫做 size 的變數 (不是 integral type 還能是什麼?) 誤判為一個 ifstream 的 instance
  • 視 strong-typed compiler (如 C++ 編譯器) 為低能. 認為 compiler 有可能會 – 在你把一個叫 name 的變數 (不是某種字串還能是什麼?) 當作 ComboBox 來操作時, 不產生 compile error

我常用的命名法則是盡量口語化, 並試著恰如其分地命名. 舉例說明較快:

  • Instance of integral type
    • unsigned long user_count;
      int offset;
      size_t index, received_bytes;
  • User-defined type
    • class Contact;
      class FastString;
      typedef list<Contact> Contacts; // or ContactList
  • Instance of user-defined type
    • string english_name;
      Contacts kens_contacts; // or contacts_of_ken
      Window main_window;

誤判以上所舉例子型別的人, 或許該考慮換個工作 :( .

當然, 上例都是淺顯的例子, 從我個人經驗來說, 應用這原則倒也沒遇過特別困難的狀況. 我想說的並不是我用的方法多好, 而是希望別再有更多人強迫他人或被他人強迫用什麼彆腳的 dwData, wndMain, ctrlBrowser, nCount, szName 來寫 C++.

[Update] Sutter on Hungarian Notation.

C++ Terminology – 0. 前序

下列 terminology (或是 term, 專有名詞, 專用名詞) 不是用來寫 code, coding 時用的是 keyword. Terminology 是被用在描述, 譬如口述, 討論或 design document 中.

這些 term (當然還有沒在此列舉到的) 看似類似, 或有些模擬兩可, 實際上卻 (在某個程度上) 各自有明確的定義. 有些term有時也讓我抓了抓頭皮, 想了想才搞清楚到底那個是那個. 當然, 肯定也會有人比我猛, 不管任何人再行, 自有一山比他高, 下面這些一個都能不倒他(她):

  • POD vs. Class vs. User-defined Type vs. Type,
  • Instance vs. Object,
  • Class Template vs. Template Class,
  • Member Template vs. Template Member vs. Class Template Member vs. Template Class Member,
  • Specialization vs. Partial Sepcialization.

在進入其他更深入的題材 (或許也只是牢騷或是閒扯蛋) 之前, 建議有興趣的人先把它們大致上搞清楚. 如果你沒好書或好導師的話, 就先將就我所知的定義. 已經準備好部份草稿, 整理一下會陸續 blog 起來. 暫時還沒計畫要將專有名詞這主題分幾次 blog, 也沒有預計 blog 與 blog 之間的順序與間隔. 就隨性吧.

至於被我主觀地認為屬於入門的題材, 如 virtual function, copy-constructable, value type, function overload/override 等等, 應該是不會有什麼著墨. 如果你搞不清楚這些, 建議你別看我的 blog, 先看看書練練功, 以後再來玩吧.

如有同好對我的 blog 有建議或指正, 也請別對我客氣.