Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) static_cast

 

static_cast is a keyword to cast related data types, for example an int to double.

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppStatic_cast.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

 

 

 

 

 

main.cpp

 

#include <cassert>

int main()
{
  //static_cast from double to int
  {
    const double x = 42.0;
    const int y = static_cast<int>(x);
    assert(y == 42);
  }
  //static_cast from double to int truncates the double
  {
    const double x = 42.999;
    const int y = static_cast<int>(x);
    assert(y == 42);
  }
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml