LearnFrenzy
Sign in Take a free test

Static and Friend Members · Static and Friend MCQ · Question 18

Why can't operator<< be a member of your class? Answer, explanation & discussion

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

Why is operator<< for printing your class written as a non-member (often a friend)?

  • A Members cannot return references
  • B In std::cout << obj the left operand is the stream; a member operator would need your object on the left (obj << std::cout) Correct
  • C It must be static
  • D Streams cannot be passed to members
Correct Answer: Option B

Making it a friend lets it read private members without public getters.

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