EXPLORATION 29

image

Custom Types

One of the key design goals for C++ was that you should be able to define brand-new types that look and act similar to the built-in types. Do you need tri-state logic? Write your own tribool type. Need arbitrary-precision arithmetic? Write your own bigint type. This Exploration introduces some of the language features that let you define custom types. Subsequent Explorations delve deeper into these topics.

Defining a New Type

Let’s consider a scenario in which you want to define a type, rational, to represent rational numbers (fractions). A rational number has a numerator and a denominator, both integers. Ideally, ...

Get Exploring C++ 11, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.