Implicit conversion

suggest change

static_cast can perform any implicit conversion. This use of static_cast can occasionally be useful, such as in the following examples:

const double x = 3.14;
printf("%d\n", static_cast<int>(x)); // prints 3
// printf("%d\n", x); // undefined behaviour; printf is expecting an int here
// alternative:
// const int y = x; printf("%d\n", y);

Without the explicit type conversion, a double object would be passed to the ellipsis, and undefined behaviour would occur.

struct Base { /* ... */ };
struct Derived : Base {
    Derived& operator=(const Derived& other) {
        static_cast<Base&>(*this) = other;
        // alternative:
        // Base& this_base_ref = *this; this_base_ref = other;
    }
};

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Explicit type conversions:
* Implicit conversion

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
83 Explicit type conversions
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype