Re: [C++] Overloaded operators? Wyatt on Wed, Sep 03, 1997 at 03:32:08PM -0500

From: Ric Klaren (j.klaren@STUDENT.UTWENTE.NL)
Date: 09/04/97


G'day!

On Wed, Sep 03, 1997 at 03:32:08PM -0500, Bode, Wyatt wrote:
Bode, Wyatt> How the heck do you overload the '<<' operator in C++ to be used by
Bode, Wyatt> cout?  I tried the following:

Just a quick snip from my code (with G++):

enum OperatorEnum                       /* operator types */
{ /*{{{*/
                O_UNKNOWN,
                O_MUL,
                    ......
                     O_LAST
};
char *OperatorNames[O_LAST] = { "UNKNOWN", "*" .... };

class Operator
{ /*{{{*/
public:
        Operator( int t = O_UNKNOWN ) : oper( (t < O_UNKNOWN || t >=
                                                                                                                O_LAST) ? O_UNKNOWN : OperatorEnum(t) ) {};
        // this is a way to do it.....
        friend ostream &operator<< ( ostream &s, const Operator &t )
        {
                return s << OperatorNames[t.oper];
        }
        inline bool operator==(
                                                                  OperatorEnum x ) const  { return oper == x; }
        inline bool operator!=( OperatorEnum x ) const  { return oper != x; }
        inline bool operator==( const Operator x ) const { return
                        oper == x.oper; }
        inline bool operator!=( const Operator x ) const { return oper !=
                        x.oper; }
        inline int value( void ) const { return oper; }
private:
        OperatorEnum oper;
        /*}}}*/
};

Grtnx Ric

PS This is from a mobprog implementation in C++... Would there be interest in
   a snippet (more a chunk... ;) of it? Espec. for the C++ porters??
--
-----+++++*****************************************************+++++++++-------
- Ric Klaren - ia_ric@cs.utwente.nl ------- klaren@cs.utwente.nl --------------
-----+++++*****************************************************+++++++++-------
``Why don't we just invite them to dinner and massacre them all when
they're drunk?''
``You heard the man. There's seven hundred thousand of them.''
``Ah? So it'd have to be something simple with pasta, then.''
-------------------------------------------------------------------------------


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/08/00 PST