| 1 |
#include <iostream> |
| 2 |
#include <vector> |
| 3 |
|
| 4 |
using namespace std; |
| 5 |
|
| 6 |
class Data |
| 7 |
{ |
| 8 |
friend class Wrapper; // Our Wrapper class is allowed to access our data |
| 9 |
// too. |
| 10 |
|
| 11 |
vector<int> d_v; // here are Data's data |
| 12 |
|
| 13 |
public: |
| 14 |
class Wrapper |
| 15 |
{ |
| 16 |
Data & d_data; |
| 17 |
size_t d_index; |
| 18 |
|
| 19 |
public: |
| 20 |
// For all situations in which data[x] is used as lvalue, |
| 21 |
// an operator must be defined accepting data[x] as lvalue and |
| 22 |
// all acceptable types as rvalue. Here I've defined two, but |
| 23 |
// generalizations are straightforward: |
| 24 |
|
| 25 |
|
| 26 |
Wrapper &operator=(Wrapper const &other); |
| 27 |
// Use our (*this) data as lvalue, the other's data as |
| 28 |
// rvalue. This is just one situation where our data are |
| 29 |
// used as lvalue. If data[x] += data[y] is needed too, |
| 30 |
// then that operator must be defined as well. |
| 31 |
|
| 32 |
Wrapper &operator=(int value); |
| 33 |
// Our data as lvalue. This again is but one example. To |
| 34 |
// implement data[x] += y etc, those operators must be |
| 35 |
// overloaded too. |
| 36 |
|
| 37 |
|
| 38 |
// There's only one situation where data[x] is used as rvalue: |
| 39 |
// when it's returning its data[x] element. This is |
| 40 |
// accomplished by defining the appropriate conversion |
| 41 |
// operator: |
| 42 |
|
| 43 |
operator int() const; |
| 44 |
// Our data as rvalue. Can't use things like int &n = |
| 45 |
// data[3] or int *p = &data[3] here, because the data[3] |
| 46 |
// expression is parsed as an rvalue, returning an int (in |
| 47 |
// this case). so if you want to use that, define an |
| 48 |
// explicit reference and/or pointer returning member, |
| 49 |
// like int &n = data.ref(3). |
| 50 |
|
| 51 |
private: |
| 52 |
// nobody but Data has anything to do with Wrapper's |
| 53 |
// constructor so it's made private, declaring Data as its |
| 54 |
// friend. |
| 55 |
|
| 56 |
friend class Data; |
| 57 |
|
| 58 |
Wrapper(Data &data, size_t index); |
| 59 |
}; |
| 60 |
|
| 61 |
Data(); |
| 62 |
|
| 63 |
int operator[](size_t index) const; |
| 64 |
// Always rvalue: operator[] of const objects |
| 65 |
|
| 66 |
Wrapper operator[](size_t index); |
| 67 |
// With non-const objects: the Wrapper is returned, to which |
| 68 |
// a value can be assigned, in which case it's is an lvalue, or |
| 69 |
// whose value can be requested, in which case the int-conversion |
| 70 |
// operator is called. |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
// I've defined all members inline, although you shouldn't do so in real life. |
| 75 |
|
| 76 |
|
| 77 |
inline Data::Wrapper &Data::Wrapper::operator=(Wrapper const &other) |
| 78 |
{ |
| 79 |
cout << "data[x] = data[y], lvalue = rvalue\n"; |
| 80 |
|
| 81 |
d_data.d_v[d_index] = other.d_data.d_v[other.d_index]; |
| 82 |
cout << "assignment performed\n"; |
| 83 |
|
| 84 |
return *this; |
| 85 |
} |
| 86 |
|
| 87 |
inline Data::Wrapper &Data::Wrapper::operator=(int value) |
| 88 |
{ |
| 89 |
cout << "data[x] = y, lvalue = expression\n"; |
| 90 |
|
| 91 |
d_data.d_v[d_index] = value; |
| 92 |
|
| 93 |
return *this; |
| 94 |
} |
| 95 |
|
| 96 |
inline Data::Wrapper::operator int() const |
| 97 |
{ |
| 98 |
cout << "using the conversion operator, " |
| 99 |
"returning an rvalue\n"; |
| 100 |
|
| 101 |
return d_data.d_v[d_index]; |
| 102 |
} |
| 103 |
|
| 104 |
inline Data::Wrapper::Wrapper(Data &data, size_t index) |
| 105 |
: |
| 106 |
d_data(data), |
| 107 |
d_index(index) |
| 108 |
{} |
| 109 |
|
| 110 |
inline Data::Data() |
| 111 |
: |
| 112 |
d_v(5) |
| 113 |
{ |
| 114 |
for (size_t idx = 0; idx < 5; ++idx) // assign some values |
| 115 |
d_v[idx] = 2 * idx; |
| 116 |
} |
| 117 |
|
| 118 |
inline int Data::operator[](size_t index) const |
| 119 |
{ |
| 120 |
cout << "rvalue-only from const object\n"; |
| 121 |
return d_v[index]; |
| 122 |
} |
| 123 |
|
| 124 |
inline Data::Wrapper Data::operator[](size_t index) |
| 125 |
{ |
| 126 |
return Wrapper(*this, index); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
int main() |
| 133 |
{ |
| 134 |
Data test; // a non-const object |
| 135 |
|
| 136 |
Data const testConst; // a const object (normally via, e.g., a |
| 137 |
// 'Data const &' parameter |
| 138 |
|
| 139 |
// insert some (r)values: |
| 140 |
cout << "Inserting rvalues:\n" << |
| 141 |
test[1] << " " << test[2] << "\n\n"; |
| 142 |
|
| 143 |
cout << "data[x] to data[y] assignment:\n"; |
| 144 |
test[1] = test[2]; |
| 145 |
cout << "Resulting in: " << test[1] << " " << test[2] << "\n\n"; |
| 146 |
|
| 147 |
cout << "Assignment from testConst:\n"; |
| 148 |
test[1] = testConst[4]; |
| 149 |
cout << "Resulting in: " << test[1] << "\n\n"; |
| 150 |
|
| 151 |
cout << "Direct assignment to test[0] (now " << test[0] << |
| 152 |
") as an lvalue:\n"; |
| 153 |
test[0] = 11; |
| 154 |
cout << "Resulting in: " << test[0] << "\n\n"; |
| 155 |
|
| 156 |
cout << "Using test[1] (= " << test[1] << |
| 157 |
" + 2) in an expression as rvalue\n"; |
| 158 |
int n = test[1] + 2; |
| 159 |
} |