Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why must operator[] be a non static member function? [duplicate]

Tags:

c++

The C++ Standard says that "=" , "()" , "[]" should be non static member function.

Why it is specified in this way ? Why do we need to define standard in such a manner ?

like image 454
Lalu Parshad Avatar asked Mar 04 '16 06:03

Lalu Parshad


1 Answers

I don't know what the standards committee was thinking, but these operators aren't much use if they are not operating on the object itself.

You also get into trouble if "anything" can be used for operator() or operator[], since they are also used in conventional code. If you don't need an object to operate on [and thus select the correct operator through], it gets messy to figure out which operator[] to use.

The operator= is even more so: What are you assigning, if not an object? It makes absolutely no sense to do that on anything but an object.

like image 171
Mats Petersson Avatar answered Oct 01 '22 09:10

Mats Petersson