Previous section   Next section

Imperfect C++ Practical Solutions for Real-Life Programming
By Matthew Wilson
Table of Contents
Chapter 29.  Arithmetic Types


29.8. Accessing the Value

The only question remains is how do we extract the values from instances of UInteger64, when we need to store it, or transmit it, and so forth.

Remember that we're only using the 64-bit integers as an example, so although we might choose to have an accessor method or operator providing the value in a built-in 64-bit integer type for compilers that support it, in general we'd not have that option. This is a perfect case for explicit casts (see section 19.5) if only they were portably reliable and efficient with reference types. The alternative I would choose would be to provide const and non-const get_value() methods, which return const and non-const references to the underlying structure as follows:



class UInteger64


{


  . . .


  uinteger64       &get_value();


  uinteger64 const &get_value() const;


  . . .



Although the non-const version of cast or function will allow you to manipulate the contents of the instance outside of the public interface, you'll probably, in the general case, need to be able to provide such access to C-APIs. Since UInteger64 is a simple value type that manages no resources, we're not exactly letting the burglars at the crown jewels by doing so.

If you prefer to provide them as implicit conversion operators, then you may as well just derive publicly from uinteger64, and let the compiler convert where necessary. I prefer the cautionary aspect inherent in requiring an explicit function call: it requires an explicit choice on the part of the programmer and is also much easier to search for automatically.


      Previous section   Next section