Examples
Team LiB
Previous Section Next Section

Examples

Either clients should be able to delete polymorphically using a pointer to Base, or they shouldn't. Each alternative implies a specific design:

  • Example 1: Base classes with polymorphic deletion. If polymorphic deletion should be allowed, the destructor must be public (else calling code can't call it) and it must be virtual (else calling it results in undefined behavior).

  • Example 2: Base classes without polymorphic deletion. If polymorphic deletion shouldn't be allowed, the destructor must be nonpublic (so that calling code can't call it) and should be nonvirtual (because it needn't be virtual).

Policy classes are frequently used as base classes for convenience, not for polymorphic behavior. It is recommended to make their destructors protected and nonvirtual.

    Team LiB
    Previous Section Next Section