C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Casting (void*)&foo

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Alex Vinokur
Guest





PostPosted: Tue Jun 20, 2006 9:10 am    Post subject: Casting (void*)&foo Reply with quote



Is this approach safe?

class Foo
{
// Stuff
};

void func1 (void *)
{
// Do something
}

void func2 (const Foo& foo)
{
func1 ((void*)&foo); // Is this safe
}



Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
Back to top
Robert J. Hansen
Guest





PostPosted: Tue Jun 20, 2006 9:10 am    Post subject: Re: Casting (void*)&foo Reply with quote



Alex Vinokur wrote:
Quote:
Is this approach safe?

Absolutely, for peculiar meanings of "safe". Some good rules of thumb:

* A C-style cast is never safe.
* A C-style cast to a void* is especially never safe.
* A C-style cast that disregards a const qualifier is ridiculously
especially never safe.
* And a C-style cast that tries to...

This approach is almost gratuitously unsafe. Please don't use this in
any real-world code. There are certainly some specific instances where
this approach is harmless, but in the main, this approach is just full
of all different kinds of things to avoid.
Back to top
werasm
Guest





PostPosted: Wed Jun 21, 2006 9:10 am    Post subject: Re: Casting (void*)&foo Reply with quote



Frederick Gotham wrote:

Quote:

The one major advantage of C style casts is they're not as laborious to
type out:

(int)val

Versus:

static_cast<int>(val)

Hmmm, given. One major advantage of C++ style casts is that they're
easy to find when looking for the source of your mistakes. Furthermore,
they concise. They have responsibility. They give certain guarantees.
An erroneous static_cast would fail to compile. C style casts compile
regardless of human error, and all humans make errors. For that reason,
I'll rather type .25 secs longer than look for a hard to find bug.

Regards,

Werner

Quote:


--

Frederick Gotham
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.