0
double number =10.9
int a ;
a = (int)number ;a = int(number) ;
  1. what are the difference between int() and (int) to convert de type into int
  2. if i use my own class BASE ; BASE() ; (BASE) ; what the difference?which is faster or unexpensive(cheap)?
YiFan-Ou
  • 13
  • 3
  • `int(number)` and `(int)number` are two different ways in C++ of achieving the same thing. Similar answer for user-defined classes, as long as the class supports a suitable constructor (or the expression being converted has a conversion operator). – Peter Feb 13 '22 at 08:01
  • `(int)` is the c way. `int()` is the c++ way. `xxx_cast<>()` is the modern c++ way. – zdf Feb 13 '22 at 08:06
  • @zdf - That's an opinion pushed by some. The `xxx_cast`s are more fine-grained and there are certainly use cases where they are preferable (e.g. catching some hard-to-detect errors that sail past the C-style cast or C++ function style cast). Declaring them as the "modern C++ way", particularly for a simple conversion between `double` and `int` where the errors won't occur, is bordering on zealotry though. – Peter Feb 13 '22 at 08:20
  • @Peter Right, but I do not see a simpler way to explain it. – zdf Feb 13 '22 at 08:24

0 Answers0