Liskov’s substitution principle in C++ <

Part I of III: The LSP and value assignment.

Abstract:
Liskov’s principle of substitution, a principle of generally good class design for polymorphism, strongly implies the slicing behavior of C++ value assignment. Destination value slicing can cause a partial assignment, which can easily break data integrity. Three solutions are (1) static checking of assignment, (2) dynamic checking of assignment, and (3), generally easiest, prohibiting assignment, then possibly providing cloning functionality as an alternative.

Contents:

  • > A short review of the LSP.
  • > How the LSP (almost) forces slicing in C++.
  • > The danger of slicing: type constraint violations.
  • > The possible design alternatives wrt. providing value assignment.
  • > How to provide assignment with STATIC CHECKING of type constraints.
  • > How to provide assignment with DYNAMIC CHECKING of type constraints.
  • > How to provide CLONING as an alternative to assignment.

Continue reading