0

Possible Duplicate:
Why are some operators in C++ only allowed to be overloaded as member functions?

Why operator () [] -> = must be non-static member? Why it can't be friend?

Community
  • 1
  • 1
Noro
  • 1,643
  • 5
  • 22
  • 38
  • Is there any operator that can be a static member? – Josh Lee Dec 09 '10 at 06:44
  • @jleedev: Yes, +, -, /, *... you get it. – Ed S. Dec 09 '10 at 06:45
  • @jleedev: It's common to make insertion and extraction operators that aren't members at all, much less static members, of the class they're defined with. Other operators can do the same thing. – cHao Dec 09 '10 at 06:51

2 Answers2

2

Because you have to call it on an instance of a class. Take for example the -> operator. How would you propose getting a pointer to the class itself? It doesn't make much sense.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • What about non-member functions? – Abyx Dec 09 '10 at 06:49
  • Well it isn't said that `->` has to return the pointer to the class it is called on. It could return any pointer. What do you mean with “ pointers point to heap allocated memory”? Pointers can point to statically allocated memory too. As said in my comment that’s just a design decision. – nils Dec 09 '10 at 07:03
1

Because it has to be non-static. Simple design decision. Probably because C++ doesn't have the concept of class objects.

nils
  • 628
  • 3
  • 8
  • What do you mean C++ doesn't have the concept of class objects? It has classes, it has objects, and you can put objects inside other classes... am I missing something here? – Billy ONeal Dec 09 '10 at 06:52
  • 2
    Classes aren't objects themselves. They don't really exist. Try to assign a class to something. – nils Dec 09 '10 at 06:55