Rationale of enforcing some operators to be members
The four operators mentioned in the original posting, =
, ()
, ->
and []
, must indeed be implemented as non-static member functions (by respectively C++98 §13.5.3/1, §13.5.4/1, §13.5.5/1 and §13.5.6/1).
Bjarne Stroustrup's rationale was, as I recall from earlier debates on the subject, to retain some sanity in the language, i.e. having at least some things you could rely on no matter how much Someone Else has screwed up by defining non-member operators for existing classes.
I'm not sure I completely agree that the restriction really helps with that, but.
EDIT: I consulted Bjarne Stroustrup about this (he's always helpful) but it appears that the apparent inconsistencies of the rules are no more than a case of frozen historical accident. He notes that "It looks worse now than it was then because our rules for lvalues and references have changed since the overloading rules were formulated. I tried to look into this issue again a couple of years ago, but ran out of time before producing a complete proposal."
Cheers & hth.,
PS: "The Design and Evolution of C++" book is great for this kind of question, but unfortunately I don't have it.
Why can't I overload operator=?
You can overload the cast of your type to any other type (see this and other resources easily discoverable by Google). I think this is what you need here. Overloading the assignment operator is used for quite another thing in C++.
structure versus classes
...when the overloaded operator function is a member function in both
cases i.e structure as well as class...
What makes you say that? That's not true.
In case of struct
in your example, overloaded operator function is not a member. This is why it requires 2 parameters.
So, the difference has absolutely nothing to do with struct vs. class matter. The reason you have different number of parameters in these operators is that the first one is implemented as a non-member (and therefore has two explicit parameters), while the second one is implemented as member (and therefore has only one explicit parameter).
Why cannot a non-member function be used for overloading the assignment operator?
Because the default operator=
provided by the compiler (the memberwise copy one) would always take precedence. I.e. your friend operator=
would never be called.
EDIT: This answer is answering the
Whats the inherent problem/limitation in supporting = operator ?
portion of the question. The other answers here quote the portion of the standard that says you can't do it, but this is most likely why that portion of the standard was written that way.
Related Topics
How to Add and Subtract 128 Bit Integers in C or C++ If My Compiler Does Not Support Them
Faq: Why Does Dynamic_Cast Only Work If a Class Has at Least 1 Virtual Method
Find All Substring's Occurrences and Locations
How to Return Numpy.Array from Boost::Python
How to Handle Arbitrarily Large Integers
Keyboard Input & the Win32 Message Loop
How to Set the Baud Rate to 307,200 on Linux
Scope of Using Declaration Within a Namespace
Why Doesn't Reference-To-Member Exist in C++
Cuda How to Get Grid, Block, Thread Size and Parallalize Non Square Matrix Calculation
Function Prologue and Epilogue in C
Is a Constexpr Array Necessarily Odr-Used When Subscripted
How to Read from a Version Resource in Visual C++
What Happens When You Deallocate a Pointer Twice or More in C++
Get Base Class for a Type in Class Hierarchy