Skip to main content

Composite

  • Chapter
  • First Online:
Design Patterns in Modern C++
  • 4192 Accesses

Abstract

It’s a fact of life that objects are quite often composed of other objects (or, in other words, they aggregate other objects). Remember, we agreed to equate aggregation and composition at the start of this section of the book.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    To be fair, the begin()/end() duopoly is excessive; we could take a cue from Swift and define an interface with a single member such as: std::optional<T> next(). This way, you can just call next() until it gives you an empty value. Writing something like while (auto item = foo.next()) { ... }

  2. 2.

    There is, of course, no --> operator; it’s quite simply the postfix decrementfollowed by greater-than >. The effect, though, is exactly as the --> arrow suggests: in while (count --> 0) we iterate until count reaches zero. You can do similar tricks with “operators” such as <--, --->, et cetera.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2018 Dmitri Nesteruk

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

Cite this chapter

Nesteruk, D. (2018). Composite. In: Design Patterns in Modern C++. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-3603-1_8

Download citation

Publish with us

Policies and ethics