What is the difference between c-like casting and functional casting?

Possible duplicates:
C ++ Syntax Syntax
C ++: What is the difference between the function (myVar) and (function) myVar?
What is the difference between (type) and type (value)?

  b = (int) a;  // c-like cast notation
 b = int (a);  // functional notation 
+10
c ++ casting
Jan 23 '11 at 19:04
source share
4 answers

Apparently, I made a mistake in my initial section on the answer. They are roughly equivalent. Although compound type names such as long long or void * cannot directly use functional syntax (i.e. long long(val) does not work), using typedef can work around this problem.

Both excellent signs are very bad, they should be avoided. For example:

 const char c = 'a'; void *fred = (void *)(&c); 

It works, and it should not.

The initial C-style notation sometimes behaves like static_cast , sometimes like const_cast , sometimes like reinterpret_cast , or even a combination of the two depending on the particular situation in which it was used. These semantics are quite complex, and it’s not always easy to say exactly what happens in any situation.

I switched to using mostly C ++ style styles static_cast<type>(val) and never used C style casts. Based on my research on this, I am also going to stop using style functions for anything. The question " C ++ syntax syntax styles has an excellent answer (accepted) that details why.

+5
Jan 23 '11 at 19:09
source share

There is hardly any difference. Officially, the first tells the compiler that this value is an integer. This probably does not generate any additional code. A function call is an actual call that internally performs a different type. The smart compiler optimizes this so that they are actually the same.

+3
Jan 23 '11 at 19:07
source share

There is no difference. This is a matter of preference. These are old-fashioned ghosts

+2
Jan 23 '11 at 19:07
source share

It depends on where you use it and how. Those. if you have values ​​or pointers (or pointer pointers).

With C ++, you should read * _cast <> and use them instead.

+1
Jan 23 '11 at 19:09
source share



All Articles