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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:09:17+00:00 2026-05-17T15:09:17+00:00

hey, i got something that i cannot understand ,there are two types of solutions

  • 0

hey, i got something that i cannot understand ,there are two types of solutions for overloading this operator 1 is including the friend at the start of the method and the other 1 goes without the friend.
i would very much like if some1 explain whats the difference between them advantages / disadvantages.
for example overloading the operator << in class rational:

class Rational:
{
    private: int m_t,m_b;
    ...
    friend ostream& operator<<(ostream& out,const Rational& r) // option 1
    { return out << r.m_t << "/" <<r.m_b;}  // continue of option 1

    ostream& operator<<(ostream& out,const Rational& r){return r.print();} // option 2
    virtual ostream& print(ostream& out) const // continue of option 2
    { //
      return out<<m_t << "/" << m_b;
    } //
};

i was told that the second option isnt correct , if some1 can correct me about it i would much appriciate it.
thanks in advance.

  • 1 1 Answer
  • 3 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-17T15:09:18+00:00Added an answer on May 17, 2026 at 3:09 pm

    operator<< (for ostream) needs to be a free function (since the left-hand argument is a stream, not your class).

    The friend keyword makes it a free function (a free function that has access to the private members).

    However, if this functionality can be implemented in terms of the public interface, it is better to do so and just use a non-friend free function.

    class Rational:
    {
        private: int m_t,m_b;
        public:
        ...
        virtual ostream& print(ostream& out) const
        { 
          return out<<m_t << "/" << m_b;
        } 
    };
    
    ostream& operator<<(ostream& out,const Rational& r)
    {
        return r.print(out);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey there, i got this very annoying problem: I have a CheckBoxList getting items
Hey there! Here is my setting : I've got a c# application that extracts
Hey there, I've got a block of HTML that I'm going to be using
Hey! I've got a generic package that requires a config file and multiple packages
Hey, I've got an element that does a requestAction to grab records sorted by
I've got this method I'm working on (I think that's the name) and it
Hey all. Ive got a headache trying to get my head around this. I
Hey there, i am trying to make a little app that will serve multiple
There is something that has been bugging me for a while and I need
Hey there, I got a little silly question.. After a healthy dose of some

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.