LearnFrenzy
Sign in Take a free test

Encapsulation · Encapsulation MCQ · Question 17

Why must a pimpl class define its destructor in the .cpp? Answer, explanation & discussion

  • MCQ
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 17 Back to Exercise

A class holds std::unique_ptr<Impl> where Impl is only forward-declared in the header. Why must the destructor be defined in the .cpp file?

  • A Destructors are always in .cpp files
  • B unique_ptr needs the complete Impl type to delete it, and Impl is complete only in the .cpp file Correct
  • C To make it virtual
  • D It doesn't matter
Correct Answer: Option B

An implicitly generated (inline) destructor in the header would try to delete an incomplete type and fail to compile.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links