7

The following is a cast:

int foo = (int) somefloat;

However, is this considered a cast?

int foo = int( somefloat );

More importantly, if there is a difference between the two, is the resulting compiled code different?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
MarkP
  • 4,168
  • 10
  • 43
  • 84

3 Answers3

5

The second example is often called a function style cast and was added to C++ but there's no difference between the two in terms of semantics/object code.

Here's a good explanation of the reason that function style casts were added:

What exactly is or was the purpose of C++ function-style casts?

Community
  • 1
  • 1
Benj
  • 31,668
  • 17
  • 78
  • 127
2

There is no difference of result, however only first example can be used in C. In C++ you can use both.

Zaffy
  • 16,801
  • 8
  • 50
  • 77
1

Yes, this is also a cast. C++ enables this style of casting, C only has (type)expression format casts. They're equivalent.