473,594 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

assigning void * w/o cast

Hello All
Have another qn from the practice exercises. I ve done the exercise and
the birds fly member fn was called successfully. But i still cant answer
the authors qn. why this ability to assign void * has had to be avoided
in C++. I didnt even instantiate bird obj then how was i able to call a
bird member fn. Im confused.

Quote: "Qn)Create a class called bird that can fly( ) and a class rock
that can’t. Create a rock object, take its address, and
assign that to a void*. Now take the void*, assign it to a
bird* (you’ll have to use a cast), and call fly( ) through
that pointer. Is it clear why C’s permission to openly
assign via a void* (without a cast) is a “hole” in the
language, which couldn’t be propagated into C++?" End Quote.

class bird
{
public:
void fly(void);
};
void bird::fly(void) {cout<<"Birds Fly"<<endl;}
class rock{};

//!!!!!!!!!!MAIN! !!!!!!!
void main(void)
{rock rockObj;
void * voidptr=&rockOb j;
bird* birdptr=(bird*) voidptr;
//!bird* birdptr=voidptr ;//illegal
birdptr->fly();
}
Jul 22 '05 #1
5 2361
"trying_to_lear n" <no****@no.no > wrote in message
news:cm******** **@gist.usc.edu ...
Hello All
Have another qn from the practice exercises. I ve done the exercise and
the birds fly member fn was called successfully. But i still cant answer
the authors qn. why this ability to assign void * has had to be avoided in
C++. I didnt even instantiate bird obj then how was i able to call a bird
member fn. Im confused.

Quote: "Qn)Create a class called bird that can fly( ) and a class rock
that can’t. Create a rock object, take its address, and
assign that to a void*. Now take the void*, assign it to a
bird* (you’ll have to use a cast), and call fly( ) through
that pointer. Is it clear why C’s permission to openly
assign via a void* (without a cast) is a “hole” in the
language, which couldn’t be propagated into C++?" End Quote.

class bird
{
public:
void fly(void);
};
void bird::fly(void) {cout<<"Birds Fly"<<endl;}
class rock{};

//!!!!!!!!!!MAIN! !!!!!!!
void main(void)
// should be int main()
{rock rockObj;
void * voidptr=&rockOb j;
bird* birdptr=(bird*) voidptr;
//!bird* birdptr=voidptr ;//illegal
birdptr->fly();
}


You just made a rock fly. Congratulations !

This worked because class bird doesn't have any associated data used in
bird::fly(). What this points out is that methods of C++ functions aren't
really "in the object". There is only one method which serves all objects of
that type. In other words, in C it is like this:

struct bird_data {};
void bird_fly(bird_d ata*) {cout<<"Birds Fly"<<endl;}

Although you don't see any argument in bird::fly, there is a hidden one
called "this" which points to the associated data. Since bird::fly doesn't
use its "this" pointer (just as bird_fly doesn't use bird_data) the function
worked. (It's still undefined behavior though, so kids, don't try this at
home.)

Your instructor is quite right to call void pointers a hole in the C++ type
system. That's what they are supposed to be, but if you use them you make
make rocks fly by accident. So don't use void pointers unless you need them
for legacy code or very low level operating system operations.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #2
"trying_to_lear n" <no****@no.no > wrote in message
news:cm******** **@gist.usc.edu ...
Hello All
Have another qn from the practice exercises. I ve done the exercise and
the birds fly member fn was called successfully. But i still cant answer
the authors qn. why this ability to assign void * has had to be avoided
in C++. I didnt even instantiate bird obj then how was i able to call a
bird member fn. Im confused.
It was just an accident of the way you created the classes that the call
worked. You didn't do anything in the function that depended on any
properties of the object or its class.
Quote: "Qn)Create a class called bird that can fly( ) and a class rock
that can’t. Create a rock object, take its address, and
assign that to a void*. Now take the void*, assign it to a
bird* (you’ll have to use a cast), and call fly( ) through
that pointer. Is it clear why C’s permission to openly
assign via a void* (without a cast) is a “hole” in the
language, which couldn’t be propagated into C++?" End Quote.


The reason is that it shouldn't be possible to make the rock fly without the
compiler giving an error or your forcing the rock to attempt to fly (by
using a cast). If you didn't need the cast you could accidentally (or
deliberately) do things that are wrong or dangerous without the compiler
giving an error.

DW

Jul 22 '05 #3
"Cy Edmunds" <ce******@spaml ess.rochester.r r.com> wrote in message
news:eI******** *************@t wister.nyroc.rr .com...
Your instructor is quite right to call void pointers a hole in the C++ type system. That's what they are supposed to be, but if you use them you make
make rocks fly by accident. So don't use void pointers unless you need them for legacy code or very low level operating system operations.


So maybe we should codify the advice about void pointers as:
"Don't throw rocks (they can break your code)."

:-)

-Mike
Jul 22 '05 #4

"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:CT******** **********@news read1.news.pas. earthlink.net.. .
"Cy Edmunds" <ce******@spaml ess.rochester.r r.com> wrote in message
news:eI******** *************@t wister.nyroc.rr .com...
Your instructor is quite right to call void pointers a hole in the C++

type
system. That's what they are supposed to be, but if you use them you make
make rocks fly by accident. So don't use void pointers unless you need

them
for legacy code or very low level operating system operations.


So maybe we should codify the advice about void pointers as:
"Don't throw rocks (they can break your code)."


Actually throwing rocks should be fine, it's throwing birds that could
be problematic.
Jul 22 '05 #5
Duane wrote:
"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:CT******** **********@news read1.news.pas. earthlink.net.. .
"Cy Edmunds" <ce******@spaml ess.rochester.r r.com> wrote in message
news:eI****** *************** @twister.nyroc. rr.com...
Your instructor is quite right to call void pointers a hole in the C++


type
system. That's what they are supposed to be, but if you use them you make
make rocks fly by accident. So don't use void pointers unless you need


them
for legacy code or very low level operating system operations.


So maybe we should codify the advice about void pointers as:
"Don't throw rocks (they can break your code)."

Actually throwing rocks should be fine, it's throwing birds that could
be problematic.


Is that because it's easier to catch a rock than a bird?
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2428
by: He Shiming | last post by:
Hi, I'm having a little bit of trouble regarding pointer casting in my program. I don't understand why the following two cases produce different results. Case 1: IInterface *pInterface = new CImplementation(); pInterface->Method(); Case 2:
5
8710
by: verec | last post by:
I just do not understand this error. Am I misusing dynamic_cast ? What I want to do is to have a single template construct (with no optional argument) so that it works for whatever T I want to use it with. Now, if that T happens to be some subclass of a known base class (object, in this case), I want to perform some extra stuff ... I've read Faq#35, and the most natural solution would have
1
6903
by: George Marsaglia | last post by:
The essence of a multiply-with-carry RNG is to have declarations in the RNG proc, such as unsigned long mwc( ){ static unsigned long x = 123456789, c = 362436; unsigned long long t, a = 916905990LL; \* then reatedly form the 64-bit t = a*x+c, after which the new carry c is the top 32, and the new x (output), is the bottom 32 bits of t:
188
17297
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
4
2628
by: LordHog | last post by:
Hello all, I am having a little problem getting my (void *) assignment to work correctly. Basically it is some code for a circular queue buffer I am trying to implement, but getting the deferrencing of the (void *) type is giving me problems... For brevity sake not all the functions are defined, but the name gives a clue to what it does...
31
3832
by: Twister | last post by:
Hi All, I have a question which might sound very basic. I have a simple structure: struct simple{ void *buffer; }; typedef struct simple Simple;
49
2770
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code ;-): char *cp; void *vp; void **vpp;
10
2864
by: kkirtac | last post by:
Hi, i have a void pointer and i cast it to an appropriate known type before using. The types which i cast this void* to are, from the Intel's open source computer vision library. Here is my piece of non- working code: CvMat *rowVect = cvCreateMat(1,nfeatures,CV_MAKETYPE(images- //rowVect is a pointer to CvMat type cvReshape( images, rowVect, 0, 1 ); //vectorize the image; readrow is a 1xN matrix(vector)
8
3618
by: brad2000 | last post by:
I was doing a little bit of reading in the ISO C spec. about typecasting to a void type. This caused me to have a question. In particular, I'm curious to know about section 6.3.2.2 where the specs says "is evaluated as a void expression, its value or designator is discarded." If I do the following: #include <stdio.h>
0
7876
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,...
0
8251
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8003
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
5408
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
3897
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2385
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 we have to send another system
1
1478
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1210
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.