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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:00:22+00:00 2026-05-30T23:00:22+00:00

I hav been going over some old hw assignments from a class last semester.

  • 0

I hav been going over some old hw assignments from a class last semester.
This was a given print function to print out linked list objects.
I don’t understand why the overloaded operator takes two parameters and one being an
os object. When we were printing out actual linked list objects on main.cpp, we didn’t
need to pass an os object. Also, why is it returning os? Why can’t we just use cout
instead of “os <<” ?

Thank you!

template <class T>
void List<T>::print(ostream & os) const
{
    os << "<";
    ListNode * curr = head;
    while (curr != NULL) {
        os << " " << curr->data;
        curr = curr->next;
    }
    os << " >";
}



// overloaded operator<<
template <class T>
ostream & operator<<(ostream & os, const List<T> & list)
{
    list.print(os);
    return os;
}
  • 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-30T23:00:23+00:00Added an answer on May 30, 2026 at 11:00 pm

    By the way the question was asked and how basic it is, I’m going to try to give a very simplistic (albeit rather informal and not so pedantic) answer.

    I don’t understand why the overloaded operator takes two parameters
    and one being an os object

    operator<< is a binary operator. It has a left-hand side and a right-hand side. When you write:

    cout << 123;
    

    You are invoking this operator with two operands (arguments): ‘cout’ on the left and an integer, ‘123’, on the right.

    When we were printing out actual linked list objects on main.cpp, we
    didn’t need to pass an os object.

    Your print function is a member function or operator of a class. That would implicitly deduce that the first argument, crudely speaking, does not need to be explicitly passed since you already have the ‘this’ pointer to work with for your list object. That’s not the case with non-member operators as you don’t have an implicitly deduced ‘this’ object to work with already for the left-hand side operand.

    When you write code like this:

    my_list.print(cout);
    

    You can think of it as actually passing in two arguments, ‘my_list’ and ‘cout’. Even though you don’t write it explicitly, you have access to ‘my_list’ through ‘this’ along with its members. That’s not the case if you wrote the print function as a non-member, like so:

    template <class T>
    void print(const List<T>& my_list, ostream& os);
    

    That’s also the case with your operator which is not a member function.

    Also, why is it returning os?

    Returning a reference to ostream is what allows us to write statements like this:

    cout << "hello " << "world";
    

    First we invoke operator<<(cout, “hello “) which then gives us another ostream reference to work with which then allows us to proceed to invoke operator<<(cout, “world”). If it returned void, for example, it would not allow us to invoke that operator twice in one statement since we’d be trying to output “world ” with void as the left-hand operand.

    Why can’t we just use cout instead of “os <<” ?

    cout basically implements the ostream interface. So does ofstream, ostringstream, and other types of output streams. By writing it in terms of the basic interface required and not some specific ostream derivative, you allow the code you write to work with stdio streams, file streams, stream streams, and others. Basically it makes your code very general and reusable which is something you should strive to do when practical. You’ll learn about this subject more as you tackle the concept of polymorphism.

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

Sidebar

Related Questions

I hav been spending some time these days going through gcc internals. I found
everyone i hav written some code to read images from the Database in C#.net,
hi i hav a code like this in the end i m getting error
I have xml files been imported onto an FTP server. This is stored in
I might be very stupid to ask this question. But I really hav no
i am using simple_nested_form and i hav this in my customer bill form <%=
I hav problem with my Script. I use FreeBSD and SSH to start this
I hav especific need of removing word from string, But I am having problem
I hav a base class which is abstract. I am inheritting a new class
I hav a simple query SELECT id from table1 I want to ad 20000

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.