1

Possible Duplicate:
Rationale of enforcing some operators to be members

The operators [] , -> , = cannot be overloaded as static functions...why???

Community
  • 1
  • 1
gl118
  • 11
  • 1
  • 1
    What do you mean with "as static functions"? Do you mean as _free_ functions (as opposed to _member_ functions)? – sbi Nov 11 '10 at 16:19

2 Answers2

0

Effectively, because the standard says so.

However, these decisions were made long before the standardization process started, probably mainly by Stroustrup alone. If you want to know what lead to the decisions for each operator, you should read The Design and Evolution of C++.

sbi
  • 219,715
  • 46
  • 258
  • 445
-2

A lot of causes.

Firstly, if it was possible, it would require huge programming expertise.
Secondly, operator overloading by itself is a thing that leads to a low readability of a program. And if you overloaded fundamental operators, than the readability dramatically goes down.
Thirdly, image you want to overloaded '->' operator. Why would you do this? No visible reason.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • Readability should go up, not down, *if* they're used sensibly. And I can imagine at least two good reasons to overload `operator->`; iterators and smart pointers. – Oliver Charlesworth Nov 11 '10 at 16:23
  • The readability increases in case when you get the right answer by your intuition. When you overload an operator you should every, just every time keep in mind that it doesn't do what it usually does. So every time you see an operator you should ask yourself a question: is it overloaded this time? – Vladimir Ivanov Nov 11 '10 at 16:28
  • If the decision was made on the basis of these arguments, then it would not be permitted to overload those operators at all. In fact, it is permitted to overload them as member functions but not as free functions. Therefore, the decision was not made on the basis of these arguments. – Steve Jessop Nov 11 '10 at 16:34
  • @Vlad, operators, like anything that requires responsiblity, are powerful and easy-to-understand **when** used in the right hands (streams and << are a good example, as are smart pointers with ->). In the wrong hands, of course, it is going to be a mess. But don't make the mistake of claiming it is "bad" in a blanket statement just because they have been misused. Given that mindset we may as well advise against driving cars, because of all the bad drivers... – Moo-Juice Nov 11 '10 at 16:40
  • @Moo-Juice: I definitely advise against driving cars. I don't drive, and taxi drivers don't write smart pointers in C++. Everyone's happy. – Steve Jessop Nov 11 '10 at 16:54
  • then why is it like we can overload "=" operator using member function but nt by friend func ??? – gl118 Nov 11 '10 at 17:00
  • then y is it lyk we can overload '=' operator via member func but not by friend func..? – gl118 Nov 11 '10 at 17:05