Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5947997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:02:39+00:00 2026-05-22T17:02:39+00:00

friend ostream& operator<<(ostream& os, MyClass& obj); I have e few questions: 1. Why do

  • 0
friend ostream& operator<<(ostream& os, MyClass& obj);

I have e few questions:
1. Why do I need to write ‘friend’?
2. Why do I need to write ‘&’ before ‘operator’, ‘os’ and ‘obj’?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:02:40+00:00Added an answer on May 22, 2026 at 5:02 pm

    The & in MyClass& makes the function take a reference to a MyClass object, not the object itself. (Similarly for the others.)

    References are lightweight to pass around, and any change you make to the obj affects the original object. Without the & you would be instructing the compiler to construct a whole new MyClass in the call, destroy it on return and throw away any changes you might have made to its internal state.

    The return of an ostream& is conventionally used to return the same ostream& that was passed in so you can write chains of shifts like cout << "hello " << 42 << endl; and have them behave the way you expect. (You could have it return something different – C++ makes it easy to completely mess with people’s expectations – but don’t do that.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have my class for example TEST in TEST.h I have friend ostream& operator<<
The error occurs when I try to do this friend std::ostream& operator<<(std::ostream& os, const
class mystring { friend ostream& operator<<(ostream &out, const mystring ss) { out << ss.s;
Im trying to call the function: friend ostream& operator<<(ostream& out, stack::myItem& theItem); that is
#include <iostream> using namespace std; class Array { friend ostream &operator<<( ostream &, const
Say I have a class like so: class Ingredient { public: friend istream& operator>>(istream&
//using namespace std; using std::ifstream; using std::ofstream; using std::cout; class Dog { friend ostream&
UPDATE: The following code gives me an error Graph.cpp: In function 'std::ostream& operator<<(std::ostream&, const
do you know, how to write signature of a function or method for operator<<
I'm trying to overload operator<< in the standard way. I have a class called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.