Topic: Precedence of overloaded "&&" vs overloaded "void*()
Author: arnstein@netcom.com (David Arnstein)
Date: Thu, 2 Dec 1993 18:04:03 GMT Raw View
The following code involves a template class that has an overloaded "void*"
conversion operator and an overloaded "&&" operator. I can't understand which
should have precedence, according to the ARM. Further explanation at end of
this post. Here's the code:
-------------------------------------------------------------------------------
#include <iostream.h>
template<class T> class Jnt
{
public:
Jnt (T val) : value(val) { }
Jnt (void) : value(0) { }
Jnt& operator= (const Jnt& j)
{ value = j.value; return *this; }
operator void* (void)
{ return &value; }
T value;
};
template<class T> Jnt<T> operator&& (const Jnt<T>& j1, const Jnt<T>& j2)
{
return Jnt<T> (j1.value * j2.value);
}
main()
{
Jnt<double> x(7);
Jnt<double> y(3);
Jnt<double> z = x && y;
cout << z.value << endl;
return 0;
}
-------------------------------------------------------------------------------
The problem is that the expression "x && y" evaluates to (int)1, and the
overloaded operator "&&" is not called. I believe that the overloaded "void*"
conversion operator is called instead.
This problem occurs using BC++ 3.1, but not when using g++. G++ calls my
overloaded "&&" operator. Naturally, this is the behavior I prefer.
Before I complain to Borland, I'd like an opinion: Does the C++ language
guarantee that my overloaded "&&" operator is called in preference to my
overloaded "void*" conversion operator? If so, then here is a BC++ bug and I
can squawk to Borland with confidence.
Incidentally, if anyone has access to BC++ 4.0, I'd like to know which
overloaded operator is activated by that compiler.
Thanks for your thoughts.
David Arnstein
arnstein@netcom.com