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:
In myreal.h:and in myreal.cppCode:const CMyReal& operator * (void);where CMyReal::Bad is a static object representing Not-A-Number.Code:const CMyReal& CMyReal::operator * (void) { if (this) return *this; else return CMyReal::Bad; }
Now, when I write:it calls my overloaded operator function and I get Not-A-Number, as expected.Code:CMyReal *myreal = NULL; float x = (myreal->operator *()).value();
But if I writeit doesn't call the overloaded operator function but tries to dereference the NULL pointer directly and I get an access violation error.Code:float x = (*myreal).value();
Similarly, (myreal->operator ->())->value() is ok, but myreal->value() fails with an access violation.
Any idea what I am doing wrong?


Reply With Quote
Bookmarks