In chapter 12 of Accelerated C++ the authors develop a class called Str, which is a simplified version of the standard string class, and in doing so they illustrate several important principles of abstract data type definition. The main focus is on supporting implicit conversions to and from other types. By defining appropriate constructors one can support implicit conversions from other types into Str objects, and by defining conversion operators one can support implicit conversions from Str objects into other types. The authors show how the role of such conversions dictates other design decisions, such as whether some operator definitions are defined as member or nonmember functions. They also explain why in some cases it not desirable to provide conversion operators, since doing so could result in memory leaks or other hard-to-understand errors. |