LearnFrenzy
Sign in Take a free test

Polymorphism and Virtual Functions · Polymorphism MCQ · Question 19

When to use std::variant instead of virtual functions? Answer, explanation & discussion

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

When is std::variant with std::visit a better choice than a virtual class hierarchy?

  • A When new types will be added by other libraries
  • B When the set of types is closed and known in advance, values are stored directly, and new operations are added often Correct
  • C Never
  • D Only for numbers
Correct Answer: Option B

Inheritance is open to new types; variant is open to new operations (visitors). Each makes the other kind of extension harder.

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