473,625 Members | 2,649 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 1537
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******@veriz on.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
2371
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); .... .... blabla
1
1969
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 HERE ############### #!/usr/bin/perl -w my @list = ("field1 field2 field3"); sub stripws($)
2
2133
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. Reduced, the problem is that 'pass.asp' returns the respective XML in the browser (IE 6):
1
2603
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 direction. From my understanding there are 2 "ways" of handling memory: -- one (from C) is the malloc/realloc/free set of API functions -- the other one (from C++) is the new/new/delete/delete set of API functions.
82
5333
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 > emacs. I've gotten a book on lisp, and I must say lisp is the ugliest > looking language syntax wise. What is up with this: (defun(foo()). (DEFUN FOO () NIL) > What were the lisp authors thinking? Why did Stallman use lisp in
5
1627
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 operation...
0
1779
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 PostgreSQL. Now, the datagrid has functionality to allow the developer to programmatically determine what changes (if any) that have been made to the grid by the user -- i.e. determine if any new rows has been added, any rows modified or deleted.
5
1177
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 head enough I will find a way. I have toyed with the ideas of xml, SQLServer events but have found nothing so far. As this is the final stage of my project I really would appreciate any input given.
2
1719
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 FileWriter fstream = new FileWriter("out.txt"); BufferedWriter out = new BufferedWriter(fstream); out.write("Hello Java");
2
1350
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 query it changes its positon so that the complete line can be selected when clicked , rather than dragging the mouse over the query to select the query.. at first i would like to know what is it called ?? n how do i achieve this in a fire fox...
0
8189
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8354
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6116
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4192
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1499
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.