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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:10:51+00:00 2026-05-28T02:10:51+00:00

I am refreshing cpp after a long gap, trying to understand the operator overloading

  • 0

I am refreshing cpp after a long gap, trying to understand the operator overloading methods.
I tried to overload “operator<<” to output members of object. but I am unable to do so without using friend function. I am looking for a method without using friend function.

here is my class def:

class Add{
private:
int x;

public:
friend ostream& operator<<(ostream& ostr, Add const& rhs); //Method 1
void operator<<(ostream& ostr);                //Method 2
};

functions implementations

//Method 1
ostream& operator<<(ostream &ostr, Add const& rhs)
{
    ostr<<rhs.x;

return ostr;
}

//Method 2
void Add::operator<<(ostream& ostr)
{
    cout<<" using operator<< \n";
    ostr<<x;
}

calls from the main function

cout<<Obj_Add;  //calls the Method 1

Obj_Add<<cout;  //calls the Method 2

Now my question is, I would like to achieve the Method 1 type calls without using the friend function. But do not know, it is possible or not in cpp. I have tried few implementation but all are gives me compile errors. Please help me to understand the point i’m missing here.

  • 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-28T02:10:52+00:00Added an answer on May 28, 2026 at 2:10 am

    If you have public accessor functions in your class, or a stream-like one, you don’t need the friendship with operator<<:

    // v1
    class foo{
    public:
      int data() const{ return _data; }
    private:
      int _data;
    };
    
    std::ostream& operator<<(std::ostream& o, foo const& f){
      return o << f.data();
    }
    
    // v2
    class foo{
    public:
      void stream_to(std::ostream& o){
        o << _data;
      }
    private:
      int _data;
    };
    
    std::ostream& operator<<(std::ostream& o, foo const& f){
      f.stream_to(o);
      return o;
    }
    

    v2 has the added benefit of allowing stream_to to be a virtual function, which is helpful for polymorphic base-classes, so you don’t need to reimplement operator<< for every derived class, only stream_to.

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

Sidebar

Related Questions

I understand the reason behind not auto refreshing your .dbml (Linq2Sql), .edmx (Linq2Entities) or
I'm refreshing my C++ knowledge after not having used it in anger for a
Hi I'm having problem refreshing my listview after Async operation. I have a simplecursoradapter,
Update : success progress but still a problem : new data reloaded after refreshing
I have a problem refreshing a DataGridView control after Insert or Update . The
I had a problem with refreshing file list after deleting a file. When I
I'm working on an issue that a cookie disappears after refreshing the page. I'm
I wonder if refreshing page with runtime error will overload the web server. For
I'm having trouble refreshing a view after a form submit creates an Item on
It seems that one of my __doPostBack methods (on a LinkButton ) is refreshing

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.