Mail Archives: djgpp/1997/08/14/10:12:56
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 -