• 0

[C++] Overloaded Operator Associativity


Question

If I overload an operator, such as <<, what will be the associativity?

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

class Complex {
 Complex(float re = 0, float im = 0) {
   real = re;
   imag = im;
 }
 const Complex &amp; operator=(const Complex&amp; c) {
   if (this != &amp;c) {
	 real = c.real;
	 imag = c.imag;
   }
   return *this;
 }
 ostream&amp; operator &lt;&lt; (ostream&amp; out, const Complex&amp; 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.

Link to comment
https://www.neowin.net/forum/topic/447385-c-overloaded-operator-associativity/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

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?

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Ridiculous claim that the labor cost difference of $6000 annually would increase cost per phone by $200. The employees produce 3 phones per month or what?
    • Sparkle 2.20.1 by Razvan Serea Sparkle is a free, open-source Windows optimization tool designed to make your PC faster, cleaner, and more private. With Sparkle, you can easily debloat Windows by removing unnecessary apps and services, disable Microsoft tracking to enhance privacy, and apply performance tweaks to boost speed. Its cleaner removes junk and temporary files, while every change is safe and fully reversible. Sparkle also features a modern, user-friendly interface with automatic updates, making system maintenance simple. Explore over 39 tweaks, from disabling telemetry and hibernation to optimizing network and game settings, all aimed at customizing and enhancing your Windows experience. Sparkle supports Windows 10 and 11. Sparkle 2.20.1 changelog: You can now change the Animation Direction from Up, Left, or Off. Added configurable animation direction (Up, Left, Off) for improved accessibility Added TTL caching to the system info backend Refactored tweak application flow to await NvidiaProfileInspector Improved IPC listener cleanup to correctly remove specific listeners Fixed online status not updating after successful network requests Updated system info tests to support backend caching Removed electron-toolkit utils dependency in favor of internal is.dev helper Fixed unwanted files and folders being included in application bundles Download: Sparkle 2.20.1 | Portable | ~100.0 MB (Open Source) Links: Sparkle Website | Github | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Never used the G7 Pro, but I've never had a good experience with that style of d-pad and fighting games.
    • And I just bought a seat cushion for my mesh chair. The chair feels nice but the first time I sat in it with boxers, I realized I don't like the feel of mesh on my legs. 😂
    • "This Dell 27 inch 4K 120Hz IPS monitor is really cheap after a very long time" ... Lol.
  • Recent Achievements

    • Dedicated
      Asgardi earned a badge
      Dedicated
    • Conversation Starter
      jessse3334 earned a badge
      Conversation Starter
    • Reacting Well
      JuvenileDelinquent earned a badge
      Reacting Well
    • One Month Later
      Excellence2025 earned a badge
      One Month Later
    • Week One Done
      Excellence2025 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      499
    2. 2
      +Edouard
      247
    3. 3
      PsYcHoKiLLa
      153
    4. 4
      Steven P.
      84
    5. 5
      macoman
      64
  • Tell a friend

    Love Neowin? Tell a friend!