Difference between revisions of "CPP/Casting"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Created page with '==Casting.cpp== <source lang="cpp"> #include <iostream> void StaticCast(); int main(int argc, char *argv[]) { StaticCast(); return 0; } void StaticCast() { double …')
 
 
Line 27: Line 27:
 
    
 
    
 
}
 
}
 +
</source>
 +
 +
 +
==CMakeLists.txt==
 +
<source lang="cmake">
 +
cmake_minimum_required(VERSION 2.6)
 +
 +
PROJECT(Casting)
 +
 +
ADD_EXECUTABLE(Casting Casting.cpp)
 +
 
</source>
 
</source>

Latest revision as of 14:09, 30 October 2010

Casting.cpp

#include <iostream>
 
void StaticCast();
 
int main(int argc, char *argv[])
{
  StaticCast();
 
  return 0;
}
 
void StaticCast()
{
    double a = 3.4;
 
  std::cout << "double a: " << a << std::endl;
  std::cout << "int a: " << (int)a << std::endl;
  std::cout << "int a: " << int(a) << std::endl;
  std::cout << "int a: " << static_cast<int> (a) << std::endl;
 
  double b = 0.4;
  unsigned char buc = static_cast<unsigned char>(b);
  std::cout << "b: " << b << " buc: " << static_cast<unsigned int>(buc) << std::endl;
 
}


CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(Casting)
 
ADD_EXECUTABLE(Casting Casting.cpp)