The Artima Developer Community
Sponsored Link

The C++ Source
C++ Coding Standards
by Herb Sutter and Andrei Alexandrescu
November 19, 2004

<<  Page 3 of 3


34. Prefer composition to inheritance.


Avoid inheritance taxes: Inheritance is the second-tightest coupling relationship in C++, second only to friendship. Tight coupling is undesirable and should be avoided where possible. Therefore, prefer composition to inheritance unless you know that the latter truly benefits your design.


Inheritance is often overused, even by experienced developers. A sound rule of software engineering is to minimize coupling: If a relationship can be expressed in more than one way, use the weakest relationship that's practical.

Given that inheritance is nearly the strongest relationship we can express in C++, second only to friendship, it's only really appropriate when there is no equivalent weaker alternative. If you can express a class relationship using composition alone, you should prefer that.

In this context, "composition" means simply embedding a member variable of a type within another type. This way, you can hold and use the object in ways that al- low you control over the strength of the coupling.

Composition has important advantages over inheritance:

Of course, these are not arguments against inheritance per se. Inheritance affords a great deal of power, including substitutability and/or the ability to override virtual functions (see Items 36 through 39, and Exceptions below). But don't pay for what you don't need; unless you need inheritance's power, don't endure its drawbacks.


Do use public inheritance to model substitutability. (See Item 37.)

Even if you don't need to provide a substitutability relationship to all callers, you do need nonpublic inheritance if you need any of the following, in rough order from most common (the first two points) to exceedingly rare (the rest):


[Cargill92] pp. 49-65, 101-105 • [Cline99] §5.9-10, 8.11-12, 37.04 • [Dewhurst03] §95 • [Lakos96] §1.7, §6.3.1 • [McConnell93] §5 • [Stroustrup00] §24.2-3 • [Sutter00] §22-24, §26-30 • [Sutter02] §23


Note: For browsing convenience, this bibliography is also available online. The bold references are hyperlinks in the online bibliography.

[Alexandrescu02a] A. Alexandrescu. "Multithreading and the C++ Type System" (InformIT website, February 2002).

[Alexandrescu04] A. Alexandrescu. "Lock-Free Data Structures" (>cite>C/C++ Users Journal, 22(10), October 2004).

[Butenhof97] D. Butenhof. Programming with POSIX Threads (Addison- Wesley, 1997).

[Cargill92] T. Cargill. C++ Programming Style (Addison-Wesley, 1992).

[Cline99] M. Cline, G. Lomow, and M. Girou. C++ FAQs (2nd Edition) (Addison-Wesley, 1999).

[Dewhurst03] S. Dewhurst. C++ Gotchas (Addison-Wesley, 2003).

[Henney00] K. Henney. "C++ Patterns: Executing Around Sequences" (EuroPLoP 2000 proceedings).

[Henney01] K. Henney. "C++ Patterns: Reference Accounting" (EuroPLoP 2001 proceedings).

[Lakos96] J. Lakos. Large-Scale C++ Software Design (Addison-Wesley, 1996).

[McConnell93] S. McConnell. Code Complete (Microsoft Press, 1993).

[Meyers97] S. Meyers. Effective C++ (2nd Edition) (Addison-Wesley, 1997).

[Meyers04] S. Meyers and A. Alexandrescu. "C++ and the Perils of Double- Checked Locking, Part 1" and "�Part 2" (Dr. Dobb's Journal, 29(7,8), July and August 2004).

[Schmidt01] D. Schmidt, M. Stal, H. Rohnert, F. Buschmann. Pattern- Oriented Software Architecture, Volume 2: Patterns for Concurrent and Networked Objects (Wiley, 2001).

[Stroustrup94] B. Stroustrup. The Design and Evolution of C++ (Addison- Wesley, 1994).

[Stroustrup00] B. Stroustrup. The C++ Programming Language (Special 3rd Edition) (Addison-Wesley, 2000).

[Sutter00] H. Sutter. Exceptional C++ (Addison-Wesley, 2000).

[Sutter02] H. Sutter. More Exceptional C++ (Addison-Wesley, 2002).

[Sutter04c] H. Sutter. "'Just Enough' Thread Safety" (C/C++ Users Journal, 22(9), September 2004).

About the Authors

Herb Sutter is the author of three highly acclaimed books, Exceptional C++ Style, Exceptional C++, and More Exceptional C++ (Addison-Wesley). He chairs the ISO C++ standards committee, and is contributing editor and columnist for C/C++ Users Journal. As a software architect for Microsoft, Sutter leads the design of the C++ language extensions for .NET programming.

Andrei Alexandrescu is the author of the award-winning book Modern C++ Design (Addison-Wesley, 2001) and is a columnist for C/C++ Uses Journal.

<<  Page 3 of 3

Sponsored Links

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use