0

What is the difference between :

double x = 10.3;
int y;
y = (int) x;    // c-like cast notation

And :

double x = 10.3;
int y;
y = reinterpret_cast<int>(x)   
Don Wakefield
  • 8,693
  • 3
  • 36
  • 54
  • A language tag would be good here. C++? –  Apr 10 '14 at 21:42
  • For reference, the doc page: http://msdn.microsoft.com/en-us/library/e0w9f63b.aspx. –  Apr 10 '14 at 21:42
  • Or check this answer: [Regular cast vs. static_cast vs. dynamic_cast](http://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast?rq=1) which also includes reinterpret_cast – jsantander Apr 10 '14 at 21:50
  • To put it simply, C-style cast can do nearly everything. static_cast is limited to less dangerous forms of casting, so you are less likely to make a bad cast by accident. reinterpret_cast is most similar to C-style cast and shouldn't be used for number type conversion. – Neil Kirk Apr 10 '14 at 21:59

1 Answers1

3

A C-style cast can be any of the following types of casts:

  • const_cast
  • static_cast
  • static_cast followed by a const_cast
  • reinterpret_cast
  • reinterpret_cast followed by a const_cast

the first one from that list that can be done is the what the C-style cast will perform (from C++03 5.4: "Explicit type conversion (cast notation)"

So for your example:

double x = 10.3;
int y;
y = (int) x;

the type of cast used would be a static_cast.

And y = reinterpret_cast<int>(x); won't compile.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760