C++ casting

Instead of the ancient  “(typename) thingy” use:
In a hurry:
“typename(thingy)”

Better:

  • when bits are correct for typename “reinterpret_cast<typename>(thingy)
  • when actual conversion is going to occur such as int to float. Sometimes need when there is ambiguously overloaded functions. “static_cast<typename>(thingy)
  • when mucking around with runtime type stuff: “dynamic_cast<typename>(thingy)”but such usage usually is a failure to architect the hierarchy properly, you should fix the hierarchy when time permits.

    In all cases these latter are superior to C’s original format as they wrap parens around what is being cast, making for easier reading. Casting with the exception of reinterpret_casts actually does involve a function call, the source might as well reflect that.

  • Advertisements

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s