operator overloading

This post will introduce how to overload different operators in C++ language.

Some important operators

  • input and output operators: <<, >>.
  • assignment operator: =.
  • subscript operator: [].
  • pre- and post- increament operator: ++, --.
  • call operator: ().
simple operators overloading
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
using namespace std;
class CA {
public:
// constructor and distructor.
CA(): count(0), book("") {}
CA(int cnt): count(cnt),book("") {}
CA(string str);
~CA() {};
// copy constructor
CA(const CA& C);
// IO '<<' and '>>' overloading.
// They should be friend function and not belong to this class.
friend ostream& operator<<( ostream& os, const CA& C);
friend istream& operator>>( istream& in, CA& C);
// Logical operators '==', '!=' overloading.
bool operator == (const CA& C);
bool operator != (const CA& C);
// Subscript operator '[]'
string& operator [] (const size_t index);
const string& operator [] (const size_t index) const;
// Pre-increment operators.
CA& operator ++ ();
CA& operator -- ();
// Post-increment operators.
CA operator ++ (int);
CA operator -- (int);
// Call operator.
int operator () (vector<string> books);
private:
int count; // number of books in bks.
string book; // the last books in bks.
vector<string> bks; // container of books.
};
CA::CA(string str) {
this->count = 1;
this->book = str;
this->bks.push_back(str);
}
// copy constructor
CA::CA(const CA& C) {
this->count = C.count;
this->book = C.book;
this->bks = C.bks;
}
// '<<' operator overloading.
ostream& operator<<( ostream& os, const CA& C) {
os << C.count << ", last="<<C.book << " , books = ";
vector<string> books(C.bks);
vector<string>::iterator iter;
for(iter=books.begin(); iter != books.end(); ++iter)
os << *iter << ", " ;
os << "\n";
}
// '>>' operator overloading.
istream& operator>>( istream& in, CA& C) {
in >> C.book;
if(in) { // check if in is valid.
C.bks.push_back(C.book);
++C.count;
} else {
cout << "---- input over ! -----" << endl;
}
return in;
}
// '==' operator.
bool CA::operator == (const CA& C) {
if((this->count == C.count) && (this->book == C.book))
return true;
return false;
}
bool CA::operator != (const CA& C) {
if(*this == C)
return false;
else
return true;
}
// '[]' operator.
string& CA::operator [] (const size_t index) {
return this->bks[index];
}
// pre-increment
// return the reference of this object.
CA& CA::operator ++ () {
++this->count;
this->book = "-no-";
this->bks.push_back(string("-no-"));
return *this;
}
// post-increment
// return a new object constructed from this object.
CA CA::operator ++ (int) {
CA B(*this);
++this->count;
this->book = "-no-";
this->bks.push_back(string("-no-"));
return B;
}
CA& CA::operator -- () {
--this->count;
this->bks.pop_back();
size_t i = this->bks.end() - this->bks.begin();
cout << " i= " << i << endl;
this->book = (*this)[i-1];
return *this;
}
CA CA::operator -- (int) {
CA B(*this);
--this->count;
this->bks.pop_back();
size_t i = this->bks.end() - this->bks.begin();
cout << " i= " << i << endl;
this->book = (*this)[i-1];
return B;
}
// call operator
int CA::operator () (vector<string> books) {
vector<string>::iterator iter = books.begin();
for (; iter != books.end(); ++iter) {
this->count++;
this->bks.push_back(*iter);
}
if (iter != books.begin())
this->book = *(--iter);
return this->count;
}

Dereference operator

  • Arrow operator ->:
    • Example: a->b.
    • If a is a pointer pointing to an object has member b, then return b.
    • If a is an object that has overloaded -> operator, then call operator ->(). When this return a pointer then run as above.
arrow operator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <stdlib.h>
using namespace std;
class A {
public:
void Printf() {cout << "I am A " << endl;}
};
class B {
public:
void Printf() {cout << "I am B " << endl;}
A* operator->() { return &m_A;}
private:
A m_A;
};
class D {
public:
void Printf() {cout << "I am C " << endl;}
B* operator->() { return &m_B; }
private:
B m_B;
};
class E {
public:
void Printf() {cout << "I am E " << endl;}
B& operator->() { return m_B; }
private:
B m_B;
};
int main() {
D d;
d->Printf(); // I am B (d->() return pointer to B).
d.operator->()->Printf(); // I am B (d.operator->() return pointer to B).
d->operator->()->Printf(); // I am A (d->() return pointer to B, d->operator->() return pointer to A,
// d->operator->()->Printf() return Printf() of A).
E e;
e->Printf(); // I am A (e->() return ref of B, B->() return pointer to A,
// e->Printf() return Printf() of A).
return 0;
}
  • Derecerence operator: *
deref operator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <stdlib.h>
using namespace std;
class CA;
// smart pointer
class C_Ptr {
C_Ptr(int *p): ip(p), use(1) { cout<< " init: use = " << use << ", *ip=" << *ip<<endl;}
~C_Ptr() {}
int *ip; // pointer
int use; // counter
friend class CA;
};
class CA {
public:
CA(int*p, int v): ptr(new C_Ptr(p)), val(v) {}
~CA() {
cout << "~CA(): use = " << ptr->use << endl;
if(--ptr->use == 0) {
cout << "free C_Ptr" <<endl;
delete ptr;
}
}
int& operator*() { return *ptr->ip;} // deref operator
private:
C_Ptr *ptr;
int val;
};