c++ - Are there differences between explicit type conversion "operators" in case of basic types? -


simple thing: "convert" e.g. float double. there 3 ways known me:

float v = 4.2f; 
  1. double u = (double)v;
  2. double u = double(v);
  3. double u = static_cast<double>(v);
  4. double u(v); edit: thought fourth option!

are these identical or there subtle differences? suggest use?

note question related basic types int, char, float, ... not pointers, pods or classes.

double u = (double)v; , double u = static_cast<double>(v); equivalent because both cases standard conversion used. double u = double(v); creates temporary double object (which can optimized away anyway) used initialize u. since temporaty created anyway, using 3 kinds of casts, yeah, it's same.

from 3 static_cast should preferred. it's couple of characters more typing, in long run it's better because first of explicitly specify cast type , since casting suspicious, in vivid manner


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -