C++ Data type Explicit Type Conversion via static_cast

Introduction

To convert the value of an expression to a given type, you write the following:

static_cast<type_to_convert_to>(expression)

Here's another example of the use of static_cast<>():


#include <iostream>
#include <cmath>                       // For square root function
using std::cout;//  w  w w .  j  av a2s . c  o m

int main()
{
    double value1 {10.5};
    double value2 {15.5};
    int whole_number {static_cast<int>(value1) + static_cast<int>(value2)};

    cout << whole_number;
}



PreviousNext

Related