473,499 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

weird question

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
2 1531
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2366
by: Robert Schott | last post by:
Hi .. this is for sure the 1k question on fopen but maybe can someone tell me this weird staff if i'm writing: $H = fopen($value,"r"); while(!feof($H)) { $string = fgets($H,1024); .... .......
1
1959
by: Jim Dawson | last post by:
I was writing a subroutine to extract fields from lines of text when I ran into an issue. I have reproduced this error on Perl 5.8 on AIX, 5.8 on Linux and 5.6 on Windows. ############### CUT...
2
2127
by: holgeradelmann | last post by:
Can anyone help please: I found a nice article on the web how to write my dynamice information into an RSS-XML. But I cannot get the code to work. The problem is so simple but weird....
1
2594
by: Pelle | last post by:
Hello all, I have to admit, that the idea that occurred to me recently is weird. It was somehow inspired by the huge response on the "delete operator" thread, but goes into a somewhat different...
82
5283
by: nobody | last post by:
Howdy, Mike! mikecoxlinux@yahoo.com (Mike Cox) wrote in message news:<3d6111f1.0402271647.c20aea3@posting.google.com>... > I'm a C++ programmer, and have to use lisp because I want to use >...
5
1612
by: John Kiernan | last post by:
Hey JavaScript gurus! Here's a weird one: If I check (with an alert) this line: vStrDate2 = vDate.toLocaleString(); I get: Saturday, August 14, 2004 7:57:30 PM (as I should) but this...
0
1772
by: Greenwich Support | last post by:
Hi All, I have a very weird problem which is occurring with a VB.Net app and PostgreSQL 7.3.1. There is a form that has a standard .Net datagrid on it that contains some data from a table in...
5
1170
by: marcmc | last post by:
Is it in any way possible to have a sProc called and executed within SQLServer return its return value to an open .Net Vb application? As a programmer I beleive this can be done and if I wreck my...
2
1713
by: carlos123 | last post by:
im pretty new to java. this might sound like a weird question but! how do i add this code class FileWrite { public static void main(String args) { try{ // Create file ...
2
1342
by: Drupan c | last post by:
Hello everybody i have a weird question... if u guys have observed when we use ms sql .. on the query box when we take the mouse pointer to the corner of a single line...
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.