473,473 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Create 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=&rockObj;
bird* birdptr=(bird*)voidptr;
//!bird* birdptr=voidptr;//illegal
birdptr->fly();
}
Jul 22 '05 #1
5 2343
"trying_to_learn" <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=&rockObj;
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_data*) {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_learn" <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******@spamless.rochester.rr.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)."

:-)

-Mike
Jul 22 '05 #4

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:CT******************@newsread1.news.pas.earth link.net...
"Cy Edmunds" <ce******@spamless.rochester.rr.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.
Jul 22 '05 #5
Duane wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:CT******************@newsread1.news.pas.earth link.net...
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:eI*********************@twister.nyroc.rr.co m...
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
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...
5
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...
1
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 =...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
4
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...
31
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
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...
10
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...
8
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...
0
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
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...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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
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...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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.