operator=

Pages: 12
This is why I stick the ampersand ('&') to the end of the type name, like this:

    int& a reference to an integer
    const string& a reference to a non-modifiable string
    ostream& a reference to an output stream

Hence, when you have ostream& operator << ( ostream& outs, const foo& f ), the thing returned from the function is a reference to an output stream. (Unless you are doing something tricky, it ought to be the same as the argument.) Notice also how the second argument is const.

1
2
3
4
5
6
7
8
9
10
11
struct point
  {
  int x, y;
  point( int x = 0, int y = 0 ): x( x ), y( y ) { }
  };

ostream& operator << ( ostream& outs, const point& p )
  {
  outs << "(" << p.x << ", " << p.y << ")";
  return outs;
  }

Hope this helps.
yeah, and i found somethin' strange in my tutorial sometimes it use:

1
2
3
4
5
6
7
8
9
10
class Rational {
//...
Rational(const Rational& rational);

//between this code and...
Rational operator=(const Rational& rational);

//this one...
bool operator==(const Rational &number) const;
bool operator!=(const Rational &number) const;


i don't know what's the different...

i see you're using the member definition of the struct, is it common use of struct? just the same with class?
struct and class are the same, only struct has public scope by default, and class defaults to private.
all right, thanks bro...
Topic archived. No new replies allowed.
Pages: 12