TITLE: New cast notations adopted for C++ [ Material taken from "C++ Report", Sept. 94, Vol. 6, No. 7, pp 46-51. ] (continued info on new casts) This article discusses the new static_cast mechanism, whose syntax is roughly static_cast (expr) It was design to be used for safe conversions or conversions that are not always safe, but that ar always well-defined on any given implementation. Safe and Well-defined Conversions. Any type conversions implicitly performed by compiler (a well-behaved conversion) are candidates for static_cast: class employee {}; class manager : public employee {}; void f (manager* pm) { employee* pe = static_cast (pm); } Another candidate is the implicit type conversions defined by user-defined conversions: class string { public: operator char* (); ... }; void g (char*); void f () { string s; g (static_cast (s)); // explicit request for user-def conv } Not-so-safe Conversions. These include the conversions enumerated below. The touted advantage of using static_cast in these cases, is that it makes these conversions very visible in code. Narrowing conversions of arithmetic types extern long l; int i = static_cast (l); // can lose bits Narrowing conversions of int to enum enum e { first, second, third }; extern int i; e e1 = static_cast (i); // may lose bits Converting void* to any T* (where T is C++ class) Converting between T1* and T2* (assume class employee and manager as above) void f (employee* pe) { manager* pm1 = dynamic_cast (pe); // null if invalid manager* pm2 = static_cast (pe); // garbage if invalid } Better to use static_cast than old cast. Compiler will flag conversions between unrelated class types or pointers to such. class X {}; class Y {}; X* px; Y* y = static_cast (px); // error, unrelated classes X and Y