Reply to this topic Topic Options
[C++] Overloaded Operator Associativity
Bukkfrig
Post #1 Mar 29 2006, 02:17


Neowinian
Group Icon

Group: Registered
Posts: 2
Joined: 29-March 06
Member No.: 161,617
If I overload an operator, such as <<, what will be the associativity?

For my tests I used a simple class, defined as:

CODE
class Complex {
Complex(float re = 0, float im = 0) {
   real = re;
   imag = im;
}
const Complex & operator=(const Complex& c) {
   if (this != &c) {
     real = c.real;
     imag = c.imag;
   }
   return *this;
}
ostream& operator << (ostream& out, const Complex& z) {
   z.show(out);
   return out;
   ...
}
};


My tests have shown that operator<<(ostream& out, const Complex& z) is left-to-right associative, as is usual for the << operator. Also, operator=(const Complex &c) is right-to-left associative, as is normal for the = operator.

This leads me to believe that the associativity of my overloaded operators is based on the associativity of the default operators (or that the actual symbols used specify associativity, and not the function itself). My lecturer however, tells me that the associativity is decided by the compiler based on the return types of the overloaded operators.

Which is correct?
(or, if I've explained it poorly, just answer: How is the associativity for overloaded operators determined?)

Thanks for your help.
Profile Card PM + Reply to Post Go to the top of the page

Log In or Register · Advertise on Neowin
AaronMT
Post #2 Mar 29 2006, 03:53


Software Developer
Group Icon

Group: Registered
Posts: 2,100
Joined: 16-July 01
From: Toronto, Ontario
Member No.: 115
I've never heard of compiler specific associativity.
Profile Card PM + Reply to Post Go to the top of the page Email Poster
Bukkfrig
Post #3 Mar 29 2006, 06:14


Neowinian
Group Icon

Group: Registered
Posts: 2
Joined: 29-March 06
Member No.: 161,617
QUOTE(INFERNO2k @ Mar 29 2006, 15:53) [snapback]587361227[/snapback]

I've never heard of compiler specific associativity.


That's not really what I was getting at..

My lecturer tells me that the compiler decides based on the return types of the operators. This does not necessarily have to be compiler specific. This doesn't really make sense to me as I dont believe C++ would be written in a way that makes it unclear which operators would be invoked first.

Just to clarify, if I overload the << operator with any function, any return type, it should still be left-to-right associative, correct?

And similarly, if I overload the = operator, it should still be right-to-left associative?
Profile Card PM + Reply to Post Go to the top of the page
AndreasV
Post #4 Mar 29 2006, 10:04


Neowinian³
Group Icon

Group: Registered
Posts: 357
Joined: 1-February 04
Member No.: 46,560
The lecturer is wrong. associativity and precedence of operators is always the same, regardless the type.

The C++ FAQ seems to agree with that:
QUOTE("the C++ FAQ")
The names of, precedence of, associativity of, and arity of operators is fixed by the language.
Profile Card PM + Reply to Post Go to the top of the page Email Poster
« Older · Programming (C#, C++, JAVA, VB, .NET etc.) · Newer »
 Reply to this topic