Topic: Another Example of where a dot operator would be useful.
Author: garyp@manutius.UUCP (Gary Powell)
Date: 10 Jul 92 18:30:48 GMT Raw View
I'm not trying to agravate the situation any more but I beleive there are
other common uses for a "." operator. The one that I ran into was a
iterator class. It had no public member fns, only overloaded operators.
I was forced into overloading () and -> when I wanted to overload ".".
class Array {
public: int* datamember;
private:
... // other data.
};
class Iterator {
public:
int operator->() { return (*current_datamember);}; // had to code this.
int operator.() { return (*current_datamember);}; // I wanted this.
private:
// data to keep track of where we are in Array.
int* current_datamember;
.... // other stuff.
};
main()
{
Array a(SIZE); // a class with an array of datamembers.
Interator i(a);
for (i = 0; i < SIZE; i++) // note I overloaded = to initialize position
// and ++ to advance the iterator.
// and < to compare position with location.
{
i.datamember = 0; // wanted this
i->datamember = 0; // have to write this.
}
}
I think the . notation is much easier to read. After all I have an
instance of i, not a pointer to an i. I agree this loop hides a lot of
fn calls but that is the point. It is easy to use, not necessarily easy
to understand all the behind the scenes action. Which was one of my goals,
and easy to use interface to an array type which did bounds checking and
data validataion. A bug prevented by checking is easier to find than one
requireing the debugger.
I realize there are as many ways to write iterator class as there are C++
programers but this one fit the problem I was trying to solve.
-Gary-
Gary Powell ------------+ Internet: garyp@camco.aldus.com
c/o Aldus Corporation | uunet: manutius!garyp@uunet.uu.net
411 1st Ave South | uucp: camco!manutius!garyp
Seattle, WA 98104 USA | Voice: (206) 622-5500