By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,514 Members | 1,801 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,514 IT Pros & Developers. It's quick & easy.

weird question

P: n/a
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; }
};
Oct 27 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Todd A. Anderson wrote:
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 *.
The compiler doesn't know that you want to print a bar*. It probably simply
interprets the bits that your foo contains as the type that you specified
in the format string of printf.
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?
The compiler isn't doing any automatic conversions at all.
Variable argument lists are very limited in use. The compiler has no way of
knowing what type the function internally expects, so how would it know
what to convert an argument to? It doesn't even know how many arguments to
expect. Also, variable argument lists cannot be used with non-POD types.
If you want a specific conversion, you need to do it yourself, so you have
to cast your foo into a bar* to tell the compiler to convert it.
Alternatively use cout and operator << instead of printf, and generally
avoid the use of variable argument lists in C++.
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; }
};


Oct 27 '05 #2

P: n/a
"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

Oct 27 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.