delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/19/07:29:51

Message-Id: <199708191127.UAA28352@sadecs.nexus.edu.au>
From: "Tom Cook" <tcook AT nexus DOT edu DOT au>
To: "DJGPP mailing list." <djgpp AT delorie DOT com>
Subject: Re: C++ overloaded operators and operator preceedence
Date: Mon, 18 Aug 1997 19:30:23 +0930
MIME-Version: 1.0

 Jeff Weeks wrote:
 
 > Alright, does anybody know if operator preceedence (or, order of
 > operations) is preserved when you overload an operator in C++?
 
From the MSVC 1.00 docs:

The operator keyword declares a function specifying what operator-symbol
means when applied to instances of a class. This gives the operator more
than one meaning, or "overloads" it
. The compiler distinguishes between the different meanings of an operator
by examining the types of its operands.
Rules of Operator Overloading

You can overload the following operators:

+	-	*	/	%	^
!	=	<	>	+=	-=
^=	&=	|=	<<	>>	<<=
<=	>=	&&	||	++	--
( )	[ ]	new	delete	&	|
~	*=	/=	%=	>>=	==
!=	,	->	->*		
If an operator can be used as either a unary or a binary operator, you can
overload each use separately.
	You can overload an operator using either a nonstatic member function or a
global function that's a friend of a class. A global function must have at
least one parameter that is of class type or a reference to class type.
	If a unary operator is overloaded using a member function, it takes no
arguments. If it is overloaded using a global function, it takes one
argument.

If a binary operator is overloaded using a member function, it takes one
argument. If it is overloaded using a global function, it takes two
arguments.

Restrictions on Operator Overloading

You cannot define new operators, such as **.
	You cannot change the precedence or grouping of an operator, nor can you
change the numbers of operands it accepts.
	You cannot redefine the meaning of an operator when applied to built-in
data types.
	Overloaded operators cannot take default arguments.
	You cannot overload any preprocessor symbol, nor can you overload the
following operators:

.	.*	::	?:	:>
The assignment operator has some additional restrictions. It can be
overloaded only as a nonstatic member function, not as a friend function.
It is the only operator that cannot be inherited; a derived class cannot
use a base class's assignment operator.


Be warned, VC1.0 is OLD! This may have changed, but as far as I know it's
still right.

Someone please correct me if this is wrong.

Hope this helps.

Tom Cook

- Raw text -


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