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对我来说,我也很陌生,不过多用一些非抽象类别的程式,可能就会懂具象类别是怎么一回事了!