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 & 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?)
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
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. 😂
Question
Bukkfrig
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 & 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.
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