Re: seems we can do with static_cast in Bye::castFrom etc…

I think I was a little sleepy when I wrote that….

Now as I think I am wide awake ;-) , as it can not be guaranteed here by design ( as in CRTP ), dynamic_cast seems to be the correct choice…

( like in CRTP you cannot guarantee that packet would always be Bye, dynamic_cast is correct — as it will throw an exception if the cast is wrong )

I will probe further to see why we need castForm etc…

– cheers atul

atul wrote:

Hi all

While going thru Bye.h

inline Bye & Bye::castFrom (Packet & packet)
{ return dynamic_cast (packet); }
I think static_cast would be a better choice here…

The reason is that as the compiler can see the line
class Bye : public Packet

the static cast should be safe… ( We don’t need the heavy duty dynamic_cast here )

I mean here it may not matter much but if some cycles are saved the better…. and in other places it might make some difference.

Also dynamic_cast implies RTTI must be used and generated… here anway it is as Packet::copy is the virtual constructor stuff… but other places it could matter ;-)

I accept that safety offered by dynamic_cast is good and yes “Early optimization is the root of all evil” but still

here are my two cents … ;-)

Would you like to post a relpy?


This post is a reply to:
seems we can do with static_cast in Bye::castFrom etc…
Hi all While going thru Bye.h inline Bye & Bye::castFrom (Packet & packet) { return dynamic_cast (packet); } I think static_cast would be a betterĀ  choice here... The reason is that as the compiler (more...)

Follow-ups:
Re: Re: seems we can do with static_cast in Bye::castFrom etc…
Hi Atul, You were faster than me in replying ;-). I totally agree with you! Here is what I wanted to post right before I saw your reply. It would (more...)