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 7673555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:32:19+00:00 2026-05-31T16:32:19+00:00

I am trying to overload operator<< as a member function. It works if simply

  • 0

I am trying to overload operator<< as a member function. It works if simply do this:

friend ostream& operator<<(ostream& os, const MyClass& myClass); in my header file and in my MyClass.cc file:

ostream& operator<<(ostream& os, const MyClass& myClass)
{
   return myClass.print(os);
}

However, if I try to take the friend off and make it a member function, then it complains that operator<< can only take one argument. Why?

ostream& MyClass::operator<<(ostream& os, const MyClass& myClass)
{
   return myClass.print(os);
}

I read at this question that it can’t be a member function, but not sure why?

  • 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-31T16:32:20+00:00Added an answer on May 31, 2026 at 4:32 pm

    When overloaded as a member function, a << b is interpreted as a.operator<<(b), so it only takes one explicit parameter (with this as a hidden parameter).

    Since this requires that the overload be part of the class used as the left-hand operand, it’s not useful with normal ostreams and such. It would require that your overload be part of the ostream class, not part of your class. Since you’re not allowed to modify ostream, you can’t do that. That leaves only the global overload as an alternative.

    There is, however, a fairly widely-used pattern where you overload the operator globally, but have that call a member function:

    class whatever { 
        // make this public, or the global overload a friend.
        std::ostream &write(std::ostream &dest) const { 
            // write self to dest
        }
    };
    
    std::ostream &operator<<(std::ostream &os, whatever const &w) { 
         return w.write(os);
    }
    

    This is particularly useful when/if you want polymorphic behavior. You can’t make the overloaded operator polymorphic itself, but you make the member function it calls virtual, so it acts polymorphic anyway.

    Edit: to (I hope) clarify the situation, you can do this a few different ways. The first and probably most obvious is to just make our write member public, and have the global operator call it. Since it is public, we don’t have to do anything special to let the operator use it:

    class myClass {
    public:
        std::ostream &write(std::ostream &os) const { 
            // write stuff to stream
            return os;
        }   
    };
    
    std::ostream &operator<<(std::ostream &os, myClas const &m) { 
       // since `write` is public, we can call it without any problem.
       return m.write(os);
    }
    

    A second alternative is to make write private, and declare operator<< a friend to give it access:

    class myClass {
        // Note this is private:
        std::ostream &write(std::ostream &os) const { 
            // write stuff to stream
            return os;
        }
    
        // since `write` is private, we declare `operator<<` a friend to give it access:
        friend std::ostream &operator<<(std::ostream &, myClass const &);
    };
    
    std::ostream &operator<<(std::ostream &os, myClas const &m) { 
       return m.write(os);
    }
    

    There’s a third possibility that’s almost like the second:

    class myClass {
        // Note this is private:
        std::ostream &write(std::ostream &os) const { 
            // write stuff to stream
            return os;
        }
    
        // since `write` is private, we declare `operator<<` a friend to give it access.
        // We also implement it right here inside the class definition though:
        friend std::ostream &operator<<(std::ostream &os, myClas const &m) { 
            return m.write(os);
        }
    };
    

    This third case uses a rather strange (and little known) rule in C++ called “name injection”. The compiler knows that a friend function can’t be part of the class, so instead of defining a member function, this “injects” the name of that function into the surrounding scope (the global scope, in this case). Even though operator<< is defined inside the class definition, it’s not a member function at all — it’s a global function.

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

Sidebar

Related Questions

I'm trying to overload the comma operator with a non-friend non-member function like this:
I'm simply trying to overload a + operator and I'm getting this compiler warning
I'm trying to do it this way: template <typename T> ostream &operator<<(ostream &os, T
I am trying to overload the postfix increment operator as a member function for
I mean, I was trying to overload the operator<< inside the class like this
I'm trying to overload the operator << as a friend to a template class
I'm trying to overload the dereference operator, but compiling the following code results in
I am trying to write operator overload for custom class and don't know how
//using namespace std; using std::ifstream; using std::ofstream; using std::cout; class Dog { friend ostream&
I'm trying to overload an operator in C# (don't ask why!) that applies to

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.