LearnFrenzy
Sign in Take a free test

Constructors and Destructors · Constructors MCQ · Question 20

Which constructor do braces prefer? Answer, explanation & discussion

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

A class has Bag(int n, int v) and Bag(std::initializer_list<int>). Which runs for Bag b{3, 7};?

  • A Bag(int, int)
  • B Bag(std::initializer_list<int>) Correct
  • C Neither – ambiguous
  • D The default constructor
Correct Answer: Option B

Brace initialization strongly prefers an initializer_list constructor when one can be used. Bag b(3, 7); calls the two-int constructor – the same reason std::vector<int>{3, 7} differs from (3, 7).

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