"Todd A. Anderson" <dr******@verizon.N0SPAM.net> wrote in message
news:dj**********@news01.intel.com
Hi...any help would be appreciated. Kind of a weird scenario but
I have foo's that point to bars and whenever anybody tries to use a
foo as a pointer I want it typed as a baz * and likewise if they do
foo-> then also typed as a baz *. So I overload some operators
as shown below. In Visual Studio 6, I tried simply to printf a foo
and instead of the getting the value of the baz I got the value of
bar *. Moreover, I tried to pass that foo as one of the "..."
parameters to a function and again it was not converted to a baz but
instead the
compiler used the value of the bar *. Is there anyway I can force
the compiler to not do any automatic conversions for the foo type
other than the two operators I've defined?
class bar {
protected:
baz *z;
friend foo;
};
class foo {
protected:
bar *b;
public:
operator baz *(void) { return b->z; }
baz * operator->(void) { return b->z; }
};
It would help you get answers if your questions were less cryptic. Some
compileable code illustrating the problem would be ideal.
If memory serves, printf and other variadic (...) functions don't do type
checking and hence never invoke conversion operators. printf just looks at
the format specifiers (%d, %s etc.) and treats the arguments you supply as
being of the required type, e.g., if the format specifies a 4 byte object,
then printf just reads 4 bytes, regardless of whether the argument supplied
is 4 bytes or more or less than 4 bytes and regardless of whether it is 4
bytes of the required type. If you don't supply the right type, then
disaster can ensue.
I think your foo is being treated as a bar * simply because a bar * pointer
is the first thing in a foo object and hence that is what printf uses. If
you explicitly cast the printf argument, then that will cause the conversion
operator to be invoked.
--
John Carson