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:

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

Table Of Contents