C++ CPA study materials
C++ CPA study resources

Virtual dispatch does not repair invalid deletion

C++ CPA · Put your knowledge to work in context.

All case studies

Virtual dispatch does not repair invalid deletion

A fictional code review finds a base class with a virtual display function but no virtual destructor. A dynamically allocated derived object is stored through a base pointer. The display call selects the expected derived implementation, after which the program deletes through that base pointer. A colleague declares the program correct because the first printed word matched the intended answer. Another examines the destruction rule independently of the earlier dispatch. This written exercise does not run the invalid program or alter application code. It asks the learner to separate a virtual-function result from whole-program validity and explain why lifetime and ownership rules remain relevant even when a preliminary output looks correct.

1. What does the successful virtual call not establish?

Read the worked answer

It does not establish that deleting the derived object through this base pointer is valid. The relevant polymorphic deletion requires an appropriate virtual destructor in this ordinary scenario.

2. What should the ownership review check?

Read the worked answer

Verify dynamic object ownership, intended destruction through the interface, destructor design and supported lifetime. A preceding expected print cannot legitimize undefined behavior later.