LearnFrenzy
Sign in Take a free test

Encapsulation · Encapsulation MCQ · Question 20

How to expose a private vector read-only without copying Answer, explanation & discussion

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

Which return type gives callers read-only, no-copy access to a private std::vector<double>?

  • A std::vector&lt;double&gt;
  • B std::span&lt;const double&gt; Correct
  • C double*
  • D std::vector&lt;double&gt;&amp;
Correct Answer: Option B

A span of const elements is a view: iteration and reading are allowed, modification is not. const std::vector<double>& also works.

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