0

Code sample one:

float i=1.1f;
int j=static_cast<int>(i);
cout << j<< endl;

Code sample Two:

float i=1.1f;
int j=(int)i;
cout << j<< endl;

what's the difference between these code samples about converting in c++?

Will
  • 4,585
  • 1
  • 26
  • 48
jiafu
  • 6,338
  • 12
  • 49
  • 73
  • [This](http://www.cplusplus.com/doc/tutorial/typecasting/) might help you. – Maroun Oct 14 '13 at 06:38
  • You can read about casting in c++. Especially about `static_cast` here. – Uchia Itachi Oct 14 '13 at 06:38
  • and also: http://stackoverflow.com/questions/11213285/difference-between-static-castprimitive-typefoo-and-primitive-typefoo?rq=1 and http://stackoverflow.com/questions/103512/in-c-why-use-static-castintx-instead-of-intx – nico Oct 14 '13 at 06:40
  • What you are doing is not conversion, it's type casting (pretending a value of one type is actually a value of another type). And in practice there's no difference between the two examples. When you start working with custom classes and inheritance, the old C-way of casting might produce different results from the C++-style casting though. – Some programmer dude Oct 14 '13 at 06:40

0 Answers0