cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/14/10:12:56

Sender: crough45 AT amc DOT de
Message-Id: <97Aug14.160948gmt+0100.17070@internet01.amc.de>
Date: Thu, 14 Aug 1997 15:11:19 +0100
From: Chris Croughton <crough45 AT amc DOT de>
Mime-Version: 1.0
To: pweeks AT execulink DOT com
Cc: djgpp AT delorie DOT com
Subject: Re: C++ overloaded operators and operator preceedence

Jeff Weeks wrote:

> Alright, does anybody know if operator preceedence (or, order of
> operations) is preserved when you overload an operator in C++?

For a start, this is off-topic for this list.  Questions about
C++ language should be asked in a C++ newsgroup (and most of
them are probably answered in the FAQs for such newsgroups).

We've seen recently that asking such questions here is likely to
result in many incorrect or incomplete answers.

> For instance, if you overload the * operator, will it still be processed
> along side division in the operator preceedence?  Or will overloaded
> operators just be interpreted from left to right?

The precedence and number of arguments of operators is unchanged
when you overload them.  For instance, you can't define operator!
to be postfix or to allow something like 2!3, although those operators
which already take one or two arguments (for instance *, which is both
a unary indirection operator and a binary multiply operator) can both
be overloaded.

> In simpler terms will 3 + 2 * 2 equal 7 or 10 if both + and * are
> overloaded?

You can't overload operators for basic types.  For instance, since
there is already an operator+(int,int) you can't redefine it to do
2+2=7; one of the arguments has to be a user-defined class.

But assuming that you write a class MyInt which duplicates the
normal integer functions with the correct results, then doing

  MyInt a = 3, b = 2, c;
  c = a + b * b;

will result in 7, or 3 + (2 * 2), as normal.

Chris C

- Raw text -


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