delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/13/11:06:31

From: i96csm AT river DOT tay DOT ac DOT uk
Newsgroups: comp.os.msdos.djgpp
Subject: Re: C++ overloaded operators and operator preceedence
Message-ID: <1997Aug13.101810.4@river.tay.ac.uk>
Date: 13 Aug 97 10:18:10 +0100
References: <33EDDE0A DOT 2014C272 AT execulink DOT com> <5sn0pn$rlc$1 AT info DOT service DOT rug DOT nl> <5sp4ql$2m1 AT freenet-news DOT carleton DOT ca>
Organization: University of Abertay Dundee
Lines: 97
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <5sp4ql$2m1 AT freenet-news DOT carleton DOT ca>, ao950 AT FreeNet DOT Carleton DOT CA (Paul Derbyshire) writes:
> What exactly is the list of operators you can overload? As in, what
> combinations of nonalphabetic symbols can be used as operators? It'd be
> nice to have a full list. My C++ documentations don't specify, they say
> how to overload operators, and so forth, but don't list the symbols or
> symbol pairs that can be used legitimately as operators, as in, what is
> allowed in place of ?? in:
> 
> friend vector &operator?? (vector &a, vector &b);
> 
> and so forth.
> --
>     .*.  Where feelings are concerned, answers are rarely simple [GeneDeWeese]
>  -()  <  When I go to the theater, I always go straight to the "bag and mix"
>     `*'  bulk candy section...because variety is the spice of life... [me]
> Paul Derbyshire ao950 AT freenet DOT carleton DOT ca, http://chat.carleton.ca/~pderbysh

You can overide all the standard C/C++ operators (including cast operators)
except .  (member selection)
       :: (static member selection)
       ?: (selection)

(I think this is the list, don't flame me if its wrong)
You can not create new operators, nor change the priority of existing operators
for the autoincrement/decrement operator, postfix form ie i++ and i--
the operator take a dummy integer as the second parameter to differentaite from
the prefix form.

ie

class foo
{
public:
  operator++(); // pre-fix;
  operator++(int); // post-fix
};


BTW operators can be written as member functions, if the left hand parameter
is the class varible ie.


class foo
{
public:
  operator +(int);
}

and

class foo
{
 friend operator +(foo,int);
}

and

class foo
{
 operator +(foo,int);
}
extern operator +(foo,int);

are all the same, (for the caller) except the last one 
cannot access non-public members of 'foo'
to define the code, for this first one use

foo::operator+(int i) { /* ......... */};

and for the second and third

operator+(foo f,int i) { /* ........ */ };

For casting, use something along these lines

class foo
{
public:
 operator int();
}


this allows code like this

void goo(void)
{
  foo f;
  int i;
  i=f;
}


Colin S. Miller
i96csm AT river DOT tay DOT ac DOT uk 
 

 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019