Re: Any dot-operator proposals out there?
I've occasionally wanted to be able to override the dot-operator [...]
My search for a proposal to add support for this operator has been without success,
however, and I wonder why.
Yes, In "the design and evolution of c++" Bjarne Stroustrup describes
why he did not allow it.
If i remeber correctly the resoning goes something like this;
The biggest need for a dot operator would probably be the
implementation of wrapper objects that would behave just like the
object it wrapped. As in the dot operator would return a reference to
the underlying wrapped object and not to the wrapper itself. But then
there would be no way for a client to access any methods or data of
the wrapped object itself. We can just overload the -> operator
instead and avoid the problem.
An example:
<CODE>
#include <iostream>
template <typename T>
struct Wrapper {
Wrapper(T t):t_(t), count(0) {}
T* operator->() {
++count;
return &t_;
}
/*
T& operator.() {
++count;
return t_;
}
*/
size_t count;
T t_;
};
inline void test () {
std::string s("test");
Wrapper<std::string> ws(s);
int length = ws->length();
int count = ws.count;
/* if we had overloaded the '.' operator instead of the '->' operator,
we would have no way to get access to the 'count' member */
}
</CODE>
Regards Patrik
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]