I have a strange problem with overloading the -> and * operators. I'm trying to do this to make dereferencing a pointer to the class intrinsically safe. I think the problem is the same in both cases, so I'll describe the * operator:
class CMyReal
{
// stuff
};
const CMyReal& operator*(const CMyReal* obj)
{
if (obj)
return *obj;
else
return CMyReal::Bad;
}
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell
Unfortunately, your override of operator * in your CMyReal class only affects the use of a * when you have an object of type CMyReal. You are trying to use it on a pointer to a CMyReal, which is not the same type.
As soon as you introduce a raw pointer, you aren't going to be able to override the operator * and get a dereference of the raw pointer to call your overridden routine.
I don't know if this is at all useful to you, but you could wrap the pointer inside of a class - something like:
Now the pointer is not a raw pointer anymore, but is instead an object. The explicit call to operator * and the dereference of the myreal object both call the operator* member of CMyRealPtr.
I knew there was something wrong with my reply as I was typing it, but I couldn't think what it was. Symbian - it rots your C++ brain, you know.
jwbarton has it correct - the operator* and operator-> need to be members of a class that wraps the pointer, not members of the target class. boost::smart_ptr would be a good starting place.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell
Thanks very much for your replies. I think I understand it now. I guess this is very closely related to the 'smart pointer' templates that are available for C++. I had this idea that by simply overriding an operator, I could make my class intriniscally safe without requiring the caller to change the way it called the class and with no additional overhead. I'm really a C programmer...
I suppose the real surprise here is that the ->operator * () version worked at all. Come to think of it,
Code:
myreal->operator *()
when myreal is NULL looks decidedly dodgy
Anyway, thanks for your time.
Ian
Last edited by iangoldby; March 4th, 2004 at 03:35 AM.
Bookmarks