Operator clarification

I read on class operators and came across the following example:

class Array { public: int& operator[] (unsigned i) { if (i > 99) error(); return data[i]; } private: int data[100]; }; 

Does this mean that I can replace [ and ] with what I need? For example, can parentheses be used?

Also, what is the meaning of ampersand in int& operator[] ?


In a slightly less important note, would it be syntactically correct to use int& operator [] instead of int& operator[] ?

+4
source share
5 answers

Does this mean that I can replace [and] with what I want?

Nopes! Not all operators can be overloaded .

For example, can parentheses be used?

You can, but should not.

Also, what is the meaning of ampersand in int& operator[] ?

This means that you are returning reference to an int variable.

EDIT: In a slightly less important note, would it be syntactically correct to use int& operator [] instead of int& operator[] ?

There is no difference between them.

+3
source

What you do is define an Array class that wraps a simple integer array. By overloading the [] operator, you can access each element of the array, as well as any other array, the advantage here is that you check the upper limit to avoid buffer overflows. Ampersand means that you return a reference to an array element, it allows you to assign a value and get a value. In C ++, when you overload the [] operator, do not forget both the constant and non-constant versions:

 int& operator[] (conts int a) {...} int operator[] (conts int a) {...} const 
+2
source

You can use parentheses because there are also

 operator() 

and some libraries (like boost :: blas) do this. BUT: You cannot create a new bracket because there is no operator{} or operator<>

RE is your syntax question: I haven’t tried it, but I don’t think because operator[] is the name of the function, and you should not add spaces to the name of the function.

+1
source

While you can basically define operators as you see fit, parens will be a problem. The reason is in conflict with the class constructor! Array (25), what does this mean? I'm not sure if this is allowed (although I'm sure someone else will find out), but the fact is that even if you are allowed, you will have a problem with this.

In general, characters that serve as operators can be overloaded / redefined. You can specify how ==, &,> and [] work for your class. You cannot decide that the letter "q" now means some type of comparison. Regarding the problem (), I'm not sure.

The reference label is also necessary for indexing, because you want to potentially perform the assignment to the returned address, and not just get the value. If you allow a weak description, the "majority" of your operators will not require this.

+1
source

1) In C ++, there is no fixed set of available "overloaded" operators.
See the table on Wikipedia:
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Table
Each table has a "Overloaded" column.

2) int& indicates that you are returning a reference to the value.
C ++ references are similar to pointers and are worth their own discussion, but in the case of the operator here, it allows if you use this operator as a left assignment operand, for example

 x[3] = 0; 

This will not work if it just returns an int;

+1
source

All Articles