problem in operator overloading of >> & << ?
-
#include
using namespace std;const int size = 3;
class vector{
int v[size];public:
vector(); //constructor num vector
vector(int *x); //constructor from arr
friend vector operator * (int a, vector b); //friend 1
friend vector operator * (vector b, int a); //friend 2
friend istream & operator >> (istream &, vector &); //what is this ??
friend ostream & operator << (ostream &, vector &);
};in the above code the i am having problem understanding the friend f^n
Quote:
friend istream & operator >> (istream &, vector &);
here's what i know, so istream is for input streaming of data & ostream for output, and does & epresents some kind of reference to function >> ? It is really confusing, please explain. Thank you.
-
#include
using namespace std;const int size = 3;
class vector{
int v[size];public:
vector(); //constructor num vector
vector(int *x); //constructor from arr
friend vector operator * (int a, vector b); //friend 1
friend vector operator * (vector b, int a); //friend 2
friend istream & operator >> (istream &, vector &); //what is this ??
friend ostream & operator << (ostream &, vector &);
};in the above code the i am having problem understanding the friend f^n
Quote:
friend istream & operator >> (istream &, vector &);
here's what i know, so istream is for input streaming of data & ostream for output, and does & epresents some kind of reference to function >> ? It is really confusing, please explain. Thank you.
friend istream & operator >> (istream &, vector &);
That is just for telling the computer the extraction operator (
>>
) is friend of thevector
class. The reference (&
) is part of the extraction operator signature, e.g.istream & operator >> (istream & is, vector & v);
Because such a operator
- takes a reference to
istream
and a reference to avector
as arguments. - returns a reference to a
istream
.
Incidentally this the magic underneath the extraction operator chaining, for instance:
cin >> v >> i;
->(cin >> v) >> i;
->cin >> i;
->cin;
- takes a reference to