   Quick Typecasting Question
  Hey guys,I was watching some C++ uni lectures online and they used this syntax at the end of a functionreturn double(sum)/number;where both sum and numbers are int variables in a function that returns a double.I'm not sure if this was because I came from C then moved to C++ but to my knowlage to typecast something you would type...return (double *)sum/number;Is this just a differernt syntax for the exact same thing, is it C++ spasific?or have I only got half of a bigger picture?   
  double(sum)/number;OR((double)sum)/number;^^This creates a double out of sum (cast from int to double), and then divides that double by the integer number, which automatically casts number to a double. The result of this expression, which is returned, is also a double.(double *)sum/number;^^This creates a pointer-to-double by interpreting sum as a memory address, then tries to divide that address by an integer, which causes a compile error.(double *)(sum/number);^^This performs an integer division, then interprets the result as a memory address which is used to create a pointer-to-double. When this value is returned it will cause another compiler error as the return type is double, not pointer-to-double.   ;double(sum) is an old c-style cast. The syntax you should be using for c++ is:static_cast<type-id> (expr);//c++ Equivalent of 'return double(sum)/number;':return static_cast<double> (sum) / number;;Quote:Original post by chrisparton1991 *** Source Snippet Removed *** is an old c-style cast. The syntax you should be using for c++ is:*** Source Snippet Removed ***I think you meant (double)sumis a C-style cast.   ;Quote:Original post by chrisparton1991double(sum)is an old c-style cast. The syntax you should be using for c++ is:static_cast<type-id> (expr);//c++ Equivalent of 'return double(sum)/number;':return static_cast<double> (sum) / number;Ok, So there is a whole differernt way to cast things in C++?   ;Quote:Original post by Matty_alanQuote:Original post by chrisparton1991double(sum)is an old c-style cast. The syntax you should be using for c++ is:static_cast<type-id> (expr);//c++ Equivalent of 'return double(sum)/number;':return static_cast<double> (sum) / number;Ok, So there is a whole differernt way to cast things in C++?This might interest you: http://www.cplusplus.com/doc/tutorial/typecasting/   ;  Yeah in C++ it's encouraged to stay away from "C-style" casting. The new casts do involve more typing, but they're more explicit, which can make your code more understandable because you're writing down the reason why you're performing the cast."C-style" looks like: (type)value -- this is dangerous because it lets you do invalid casts without even warning you about it. E.g. the examples above where an integer is turned into a pointer-to-double compile without any warning/error!This is pretty simmilar to "constructor style" casting: type(value)The 'new' casts look like:static_cast<type>(value)^^This is your most common one. It converts between types (like a C-style cast) but creates a compile-time error if the cast is not valid.const_cast<type>(value)^^This lets you strip the const keyword off a type. You sometimes need this when gluing correct code together with old incorrect code, or C-code.reinterpret_cast<type>(value)^^you can use this to convert any pointer type to any other type. You sometimes need to use this when you're doing low-level bit manipulation, and you know that you're doing something dangerous.dynamic_cast<type>(value)^^This uses RTTI to cast between polymorphic types. If the cast isn't possible (e.g. trying to cast a Keyboard to a Orc) then it can fail. You can use this in object-oriented code when you're not sure if the cast is valid or not.   ;double(sum)is not really a cast. It it not allowed in C, and in C++ it is a constructor call.This is the same as how you can do:string("Hello World")Yes built-in types effectively have certain constructors.   