Delegation

Instructor's Guide
intro, inheritance, delegation, cooperation, events,

Objectives

This section shows how delegation may be employed to hide realizations, to implement smart pointers and to switch dynamically between roles.

Points to emphasize

Hints

Implicit delegation may be implemented in C++ by overloading the de-reference operator. Overloading is a powerful, yet debatable feature of C++. Discuss the trade-offs between flexibility and built-in support.

Questions

  1. Explain how the functionality of a class may be made accessible through an arbitrary interface class by employing implicit delegation.
  2. What are smart pointers? Why are they useful? How would you implement them?
  3. Sketch how you would implement dynamic role switching in C++.

Comments

Invite students to try out some simple examples themselves. How overloading the de-reference operator works in detail is in my experience quite hard to explain. Experience does wonders.