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