Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed2.atman.pl!newsfeed.atman.pl!.POSTED!not-for-mail From: "Duga Chernobyl" Newsgroups: comp.lang.c++ Subject: Re: which of these 3 casts would you prefer? Date: Fri, 26 Aug 2011 10:05:51 +0200 Organization: ATMAN - ATM S.A. Lines: 48 Message-ID: References: NNTP-Posting-Host: user-46-112-23-107.play-internet.pl Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes Content-Transfer-Encoding: Quoted-Printable X-Trace: node2.news.atman.pl 1314345954 4073 46.112.23.107 (26 Aug 2011 08:05:54 GMT) X-Complaints-To: usenet@atman.pl NNTP-Posting-Date: Fri, 26 Aug 2011 08:05:54 +0000 (UTC) User-Agent: Opera Mail/11.50 (Linux) Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:9331 Dnia 25-08-2011 o 23:58:56 A napisa=C5=82(a): > int Callback(void *pThis) > { > TMyClass *pMC =3D (TMyClass*)pThis; // C style > > TMyClass *pMC =3D static_cast(pThis); // C++ style bu= t the > // same as above > > TMyClass *pMC =3D reinterpret_cast(pThis); // Doesn't chec= k if > // input type was= > // pointer, possi= bly > // dangerous > } > > these 3 do essentially the same. is there any preferred one for void* = = > cast > like this and why? any difference in speed? > > > > IMHO there is no place for preferences. The first is used in c and should be used only in C. The second is from C++ and should be used mostly (compiler check if = conversion is usable) The third is a equivalent of the c solution. Compiler checks nothing. In c++ should be used only c++ conversion, because mostly compiler (not = in = reinterpret conv.) does some checks of common sense of this conversion. And c++ conversion are long and easy to find. The creator of c++ some ga= ve = arguments: the place of most errors is conversion ;-) dynamic_cast is used for child class pointers conversion. =