Topic: I am trying to overload the operator << to work with a class of mine:


Author: James Kuyper <kuyper@wizard.net>
Date: 1999/04/16
Raw View
Michael Frayn wrote:
>
> I am trying to overload the operator << to work with a class of mine:
>
> for ex.
>
> class Cat {
> ....other stuff
>
> friend Cat operator<< (Cat &)
> };
>
> it keeps saying that operator<< needs to have two parameters!
> what on earth is the second one?  Help!

Every overloaded operator must work the same way as the operator it's
overloading. Specifically,

 x = a<<b;

corresponds to

 x = operator<<(a,b);

Member function operator overloads take one fewer explicit argument than
the corresponding ordinary operator overload. That's because they have
an implicit first argument of 'this'.
You basically have two choices:

// Just for illustration:
typedef int argument_type;
typedef int &result_type;

class Cat{
private:
 // Features that will be visible only to member functions or
 // friend functions.
public:

#ifdef FRIEND
 friend result_type operator<<(argument_type, const Cat& c);
#else
 result_type operator<<(argument_type a);
#endif

 // Other features of Cat
}

#ifdef FRIEND
 result_type operator<<(argument_type a, const Cat& c)
 {
  // Code that uses 'a' and 'c' to calculate a return
  // value. Can access private member of 'c' because it's
  // been declared a friend.
 }
#else
 result_type Cat::operator<<(argument_type a);
 {
  // Code that uses 'this', and 'a' to calculate a
  // a return value. Can access private members of *this
  // because it's a member function.
 }
#endif

// Uses:

Cat cat();
argument_type one=1;

#ifdef FRIEND
 result_type r = one<<cat;
 // equivalent to r = operator<<(one,cat);
#else
 result_type r = cat<<one;
 // equivalent to r = cat.operator<<(one);
#endif

The two options shown above require reversed operand orders. That wasn't
necessary; I chose it to emphasize a point. If you define a binary
operator overload as a member function of 'Cat', then you have to have a
Cat object on the left hand side of the operator. If you define it as a
friend function, then you can choose either side of the operator to be a
Cat object. This is one of the key things that determines which option
to choose.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1999/04/16
Raw View
In article <F%PP2.5856$%5.1160027@news1.rdc1.on.wave.home.com>,
mfrayn@home.com says...
> I am trying to overload the operator << to work with a class of mine:
>
> for ex.
>
> class Cat {
> ....other stuff
>
> friend Cat operator<< (Cat &)
> };
>
> it keeps saying that operator<< needs to have two parameters!
> what on earth is the second one?  Help!

operator<< is binary.  If you overload it as a member function, then
the object is always the left-hand parameter.  If you overload it as a
global, then you have to provide a left-hand parameter.  Assuming
you're overloading it as an insertion operator for an ostream, the
left-hand parameter will be an ostream:

class Cat {
 friend std::ostream &operator<<(std::ostream &, Cat const &);
};

The insertion operator should normally return the ostream, NOT the
object it inserted on the ostream.  At least under normal
circumstances, it should not modify the object it inserts into the
stream, so it should receive that as a reference to a const object
unless the object is known to be small enough that passing it by value
makes sense (which is fairly unusual).

That, however, is starting to get closer to the purview of
comp.lang.c++ or comp.lang.c++.moderated, since it deal with how to
write C++ rather than what the standard says.

As far as the standard cares, just about the only restriction is that
at least one operand must be of a user-defined type; if both operands
are of built-in types, you'll either use the built-in operator, or get
a diagnostic if the built-in << operator isn't defined for those
built-in types.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Michael Frayn" <mfrayn@home.com>
Date: 1999/04/15
Raw View
I am trying to overload the operator << to work with a class of mine:

for ex.

class Cat {
....other stuff

friend Cat operator<< (Cat &)
};

it keeps saying that operator<< needs to have two parameters!
what on earth is the second one?  Help!

Michael Frayn
e-mail: mfrayn@home.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]