Overloading operators

I am reading book c++ for 21 day by jesse liberty and I didn't understand overloading operators especially I am confused for overloading operator[]. can someone please explain me something about this operator?
http://www.cplusplus.com/reference/vector/vector/operator[]/
signature and common use, ¿what you don't understand?
this is the string library from my book . how does this overloaded operator[] work?what is value int offset that operator[] is receiving??explain me a little bit please .. thanks for response anyway
1: #include <iostream.h>
2: #include <string.h>
3:
4: class String
5: {
6: public:
7: // konstruktori
8: String();
9: String(const char *const);
10: String(const String &);
11: ~String();
12:
13: // preklopljeni operatori
14: char & operator[](int offset);
15: char operator[](int offset) const;
16: String operator+(const String&);
17: void operator+=(const String&);
18: String & operator= (const String &);
19:
20: // Opsti metodi pristupa
21: int GetLen()const { return itsLen; }
22: const char * GetString() const { return itsString; }
23: // static int ConstructorCount;
24:
25: private:
26: String (int); // privatni konstruktor
27: char * itsString;
28: unsigned short itsLen;
29:
30: };
31:
32: // podrazumevani konstruktor kreira string od 0 bajtova
33: String::String()
34: {
35: itsString = new char[1];
36: itsString[0] = '\0';
37: itsLen=0;
38: // cout << "\tDefault string constructor\n";
39: // ConstructorCount++;
40: }
41:
42: // privatni (pomocni) konstruktor, koga koriste samo
43: // metodi klase za kreiranje novog stringa
44: // trazene velicine. Puni se sa null.
45: String::String(int len)
46: {
47: itsString = new char[len+1];
48: for (int i = 0; i<=len; i++)
49: itsString[i] = '\0';
50: itsLen=len;
51: // cout << "\tString(int) constructor\n";
52: // ConstructorCount++;
53: }
54:
55: // Konvertuje niz karaktera u String
56: String::String(const char * const cString)
57: {
58: itsLen = strlen(cString);
59: itsString = new char[itsLen+1];
60: for (int i = 0; i<itsLen; i++)
61: itsString[i] = cString[i];
62: itsString[itsLen]='\0';
63: // cout << "\tString(char*) constructor\n";
64: // ConstructorCount++;
65: }
66:
67: // konstruktor kopije
68: String::String (const String & rhs)
69: {
70: itsLen=rhs.GetLen();
71: itsString = new char[itsLen+1];
72: for (int i = 0; i<itsLen;i++)
73: itsString[i] = rhs[i];
74: itsString[itsLen] = '\0';
75: // cout << "\tString(String&) constructor\n";
76: // ConstructorCount++;
77: }
78:
79: // destruktor, oslobadja alociranu memoriju
80: String::~String ()
81: {
82: delete [] itsString;
83: itsLen = 0;
84: // cout << "\tString destructor\n";
85: }
86:
87: // operator jednako, oslobadja postojecu memoriju
88: // onda kopira string i velicinu
89: String& String::operator=(const String & rhs)
90: {
91: if (this == &rhs)
92: return *this;
93: delete [] itsString;
94: itsLen=rhs.GetLen();
95: itsString = new char[itsLen+1];
96: for (int i = 0; i<itsLen;i++)
97: itsString[i] = rhs[i];
98: itsString[itsLen] = '\0';
99: return *this;
100: // cout << "\tString operator=\n";
101: }
102:
103: //nekonstantni operator pomaka, vraca
104: // referencu na karakter, tako da se on moze
105: // promeniti!
106: char & String::operator[](int offset)
107: {
108: if (offset > itsLen)
109: return itsString[itsLen-1];
110: else
111: return itsString[offset];
112: }
113:
114: // konstantni operator pomaka koji se koristi
115: // sa konstantnim objektima (pogledati konstruktor kopije!)
116: char String::operator[](int offset) const
117: {
118: if (offset > itsLen)
119: return itsString[itsLen-1];
120: else
121: return itsString[offset];
122: }
123:
124: // kreira novi string dodajuci tekuci
125: // string rhs stringu
126: String String::operator+(const String& rhs)
127: {
128: int totalLen = itsLen + rhs.GetLen();
129: String temp(totalLen);
130: int i, j;
131: for (i = 0; i<itsLen; i++)
132: temp[i] = itsString[i];
133: for (j = 0; j<rhs.GetLen(); j++, i++)
134: temp[i] = rhs[j];
135: temp[totalLen]='\0';
136: return temp;
137: }
138:
139: // menja tekuci string, ne vraca nista
140: void String::operator+=(const String& rhs)
141: {
142: unsigned short rhsLen = rhs.GetLen();
143: unsigned short totalLen = itsLen + rhsLen;
144: String temp(totalLen);
145: for (int i = 0; i<itsLen; i++)
146: temp[i] = itsString[i];
147: for (int j = 0; j<rhs.GetLen(); j++, i++)
148: temp[i] = rhs[i-itsLen];
149: temp[totalLen]='\0';
150: *this = temp;
151: }
152:
153: // int String::ConstructorCount = 0;
Kristiano 96 wrote:
what is value int offset that operator[] is receiving?


The element of the char array to return;
Your compiler doesn't know how to operate on a class object that has been defined in your program unless you tell it how. For example you may try to use operators on two classes like assignment (=) or addition (+), but the class doesn't have defined behavior for how the compiler should handle such an operation.

For the standard data types we all know (char, int, double, ...) these are already defined so it may not occur to us that specifying the behavior of operators would be necessary. For the case of classes, any operations such as assignment, addition, or anything else we take for granted that we see for regularly supported data types must be defined by the programmer for that class.

In the code here the programmer is creating their own version of a string type class. The [] operator for a string (like an array or vector) allows you to access a character at a certain position in the string based on its index, where int value offset represents the index. A thought to be had: what is an index but an integer offset from the start of the array (or in this case, string, which is an array of characters)?

Check out this article, this might provide more insight into [] and why overloading this operator may be useful for the String class.
http://www.cplusplus.com/reference/array/array/operator[]/

Last edited on
Thank you very much!
Topic archived. No new replies allowed.