Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem in operator overloading of >> & << ?

problem in operator overloading of >> & << ?

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicshelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    #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.

    C 1 Reply Last reply
    0
    • T Tarun Jha

      #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.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

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

      That is just for telling the computer the extraction operator (>>) is friend of the vector 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 a vector 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;

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups