¿Qué debo elegir de las siguientes funciones?

suppose I design a String class myself and I want to overload the operator[] functions, here are the choices:

String operator[](const size_t index);
String& operator[](const size_t index);
const String& operator[](const size_t index) const;

any other combination of const, non-const and reference return type.

preguntado el 25 de agosto de 12 a las 12:08

Why would the [] operator of a string return a string? -

@Cubic Some languages do away with the string/character distinction and use single-character strings instead of a separate character type. That's no good reason to do it in C++ though. -

2 Respuestas

El [] operador de un string returns a character, not a string. Depending on circumstances, you should implement one or two of them:

const char& operator[] ( size_t index ) const;
char& operator[] ( size_t index );

The first one produces a reference that cannot be modified. If your string is immutable, that is all you need.

The second one produces a reference that can be modified. You can use it to implement clever stuff, such as copy-on-modify and reference counting.

Respondido 25 ago 12, 12:08

You should probably have both. - user1071136

@user1071136 If you implement a string that supports all standard functionality, then yes, you need both. If you want an immutable string, similar to the one in Java, C#, or Cocoa's NSString, then the first operator by itself would be sufficient. - Serguéi Kalinichenko

Just a side question. For performance, isn't it better to return a const char value instead of a const reference? As a pointer is x4 or x8 the sizeof(char). - ablm

@ablm On modern computers the difference is negligible, because CPU registers and the data bus are wide enough to not require additional cycles when passing an address vs. passing a single byte. Dereferencing of the address behind the reference will take an additional cycle, but the instructions used to implement this are highly optimized, so you wouldn't notice the difference. On the (much) older computers, such as 8088, there would be additional cycles, so returning a char would be indeed faster. - Serguéi Kalinichenko

Some people prefer to have a signed parameter type for operator[], both to be more similar to built-in operator[] (they too support negative indices) and also to be able to detect negative value arguments (in case you have an out of bounds check).

The type that the C++ compiler uses to evaluate calling the built-in operator[] is ptrdiff_t, so you will sometimes find the following

char &operator[](ptrdiff_t index);
char operator[](ptrdiff_t index) const;

I usually just use a plain int tipo de parámetro.

Respondido 25 ago 12, 12:08

suppose the return type is big data type this time. so the functions would be like this: Type &operator[](int index); and Type operator[](int index) const; so what I am thinking is would it be better return the reference of big data in that second function? if not, what would be the reason. - Honwhy Wang

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.