Difference between statement [] overload write / read?

I am new to C ++ and I am sorry about this question, but this is a struggle. If someone could help me distinguish the following lines, I would be grateful.

char& operator [](int); // write (why with reference?) char operator [](int) const; //read (why without a reference?) char const &operator[](int) const; // what is the difference compared to the previous line? const char *& operator[] (const int* ); // is this also possible? 
+5
source share
4 answers

You might want to review the operator overload overview.

So, to recall some applicable points:

  • operator[] always a non-static, unified member function.
  • There can be several overloads, as for any other member function.
  • By convention (listen to this!), Versions that do not support const will return a link to change the content (this link may be represented by a proxy type, although try to remove it).
  • By convention, const -qualified overloads can either return a const link or a copy to provide read-only access to the same elements.
    • Use const -reference if the element cannot be dynamically generated, especially if it is not trivial and cheap to copy.
    • Use the return value if the above fails.

BTW: You really want the const and non const members to be similar, so non const can be a simple built-in function delegating another with the corresponding const_cast . <w> (Do not do this the other way around, it is not entirely acceptable or safe.)

As for your last line, it indexes a pointer to const int and returns a reference to a pointer to const char .
This is a seriously odd return value and index, although if you have the right use for it, why not?

+6
source

When you overload the operator [] as char& operator [](int) , it can read / write in the base char , which is indexed by an integer number of records.

However, in many cases you need to read the properties of a const object, then you need to overload this operator using the const method, for example char operator [](int) const .

The third char const &operator[](int) const is useful the same as above, but when the base variable is const .

+2
source

The first line indicates when you change the value of the returned operator [], the old value will also change.

The second line tells you that this statement cannot change any variable, but you can change the return value and not change the old one.

The third line says that this statement cannot change any variable or return value.

The difference between the second and third line is that the first one can change the return value, the last one cannot change the return value as for the last one, I am new to C ++, so I don’t know what that means.

+2
source

and just to make sure there is an answer with some code, here are some examples of how these versions can be used:

 // Q: char& operator [](int); // write (why with reference?) // A: this is using [] to return a mutable reference to a conceptual element in my array my_type x; // x supports operator [] x[6] = 'A'; // actually modifies the 7th element of x 

-

 // Q: char operator [](int) const; //read (why without a reference?) // A: this will return a COPY of the element at [i]. // for a char this is irrelevant, a copy is trivial // if it was some large object you might want to return // a const& instead and avoid the copy char my_copy = x[6]; x[6] = 'B'; // from above // now my_copy is 'A' but x[6] is 'B' 

-

 // Q: char const &operator[](int) const; // what is the difference compared to the previous line? // A: as mentioned, for a char not a lot of difference. // For a large object it avoids a copy 

-

 // Q: const char *& operator[] (const int* ); // is this also possible? // A: yes it possible. Yes it completely evil. No, don't do it. 
+2
source

Source: https://habr.com/ru/post/1209012/


All Articles