Concrete and Abstract Types

C++ Primer 4/e在Class Definitions and Declarations這個地方有一個關鍵觀念:『Not all types need to be abstract. The library pair class is a good example of a useful, well-designed class that is concrete rather than abstract. A concrete class is a class that exposes, rather than hides, its implementation.

Some classes, such as pair, really have no abstract interface. The pair type exists to bundle two data members into a single object. There is no need or advantage to hiding the data members. Hiding the members in a class like pair would only complicate the use of the type.

Even so, such types often have member functions. In particular, it is a good idea for any class that has data members of built-in or compound type to define constructor(s) to initialize those members. The user of the class could initialize or assign to the data members but it is less error-prone for the class to do so.』

中文版的這樣寫:『並不是所有型別都要設計為抽象。C++標準庫的pair就是一個很有用而且設計良好的具象class。具象class會曝露(而非隱藏)其實作細節。

某些class如pair,並沒有抽象介面。pair用來把兩個成員變數綁為一個單獨物件。隱藏這些成員變數沒有必要,也沒有任何好處。在具象classes如pair,隱藏成員變數會使型別的使用變得複雜。

即便如此,這些型別也常常有成員函式。更明確地說,如果classes擁有內建的、或複合型別的成員變數,那麼定義建構式來初始化這些成員是個好主意。這一類classes的使用者也可以直接初始化或賦值這些成員變數,但讓classes來做比較不容易出錯。』

抽象對我來說是熟悉的,具象就真的是很陌生,如果非抽象即是具象,那我想我反而就容易懂了,可連英文concrete對我來說,我也很陌生,不過多用一些非抽象類別的程式,可能就會懂具象類別是怎麼一回事了!

感謝你看到這裡,很快就可以離開了,但最好的獎勵行動就是按一下幫我分享或留言,感恩喔~

點我分享到Facebook

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *