0
#include <iostream>
using namespace std;

const int size = 3;
class vector {
   int v[size];

    public:
        vector(); 
        vector(int *x);
        friend vector operator * (int a, vector b); 
        friend vector operator * (vector b, int a); 
        friend istream & operator >> (istream &, vector &);
        friend ostream & operator << (ostream &, vector &);
    };
}

As in the above code, i cannot grasp what

friend istream & operator >> (istream &, vector &);

Here what i know, that stream is used for input streaming of data and ostream for output but what does istream & means before operator overloadin of >>.

Jose Da Silva Gomes
  • 3,814
  • 3
  • 24
  • 34
Tarun Jha
  • 43
  • 1
  • 10
  • 3
    That line declares a function to be a friend of the class. If that does not make sense, I recommend spending some time with [a good textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and understanding the fundamentals of the language. – R Sahu Mar 15 '18 at 18:12
  • [What is a reference variable in C++](https://stackoverflow.com/q/2765999/27678) – AndyG Mar 15 '18 at 18:13
  • It means that this function that returns an `std::istream` object and takes an `std::istream` object by reference and an object of your class is a `friend` of your class. Which means it can access your `private/protected/public` data (full access). Remember a friend class or function is not a part of your class. – Raindrop7 Mar 15 '18 at 18:26

1 Answers1

-1

It means that it returns a reference to the istream object. This is necessary for operator chaining as you're used to with cout:

std::cout << "foo" << "bar";

Without returning a reference to itself, this part : << "bar", wouldn't be possible.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
  • But for that it doesn't necessarily need to make friends. Friend is only necessary if the operator needs private access to a class. This is often the case, but e.g. a completely public struct can have external operators, which (obviously) don't need to be friends with the struct. – user2328447 Mar 15 '18 at 18:36
  • @user2328447 Good thing that OP didn't ask about that then. – Hatted Rooster Mar 15 '18 at 18:43