100% found this document useful (1 vote)
44 views2 pages

Type Casting: Double Int Int Int

C++ is a strongly typed language that requires explicit type conversions between incompatible types. There are two main syntaxes for generic type casting: functional notation and C-like notation. While sufficient for fundamental data types, generic casting can cause errors when applied to class and pointer types by allowing incompatible conversions. To control class conversions, C++ provides four specific casting operators: dynamic_cast, reinterpret_cast, static_cast, and const_cast, each with their own characteristics for handling conversions.

Uploaded by

icul1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
44 views2 pages

Type Casting: Double Int Int Int

C++ is a strongly typed language that requires explicit type conversions between incompatible types. There are two main syntaxes for generic type casting: functional notation and C-like notation. While sufficient for fundamental data types, generic casting can cause errors when applied to class and pointer types by allowing incompatible conversions. To control class conversions, C++ provides four specific casting operators: dynamic_cast, reinterpret_cast, static_cast, and const_cast, each with their own characteristics for handling conversions.

Uploaded by

icul1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Type casting

C++ is a strong-typed language. Many conversions, specially those that imply a different
interpretation of the value, require an explicit conversion, known in C++ as type-casting. There
exist two main syntaxes for generic type-casting: functional and c-like:
1
2
3
4
double x = 10.3;
int y;
y = int (x); // functional notation
y = (int) x; // c-like cast notation


The functionality of these generic forms of type-casting is enough for most needs with
fundamental data types. However, these operators can be applied indiscriminately on classes and
pointers to classes, which can lead to code that -while being syntactically correct- can cause
runtime errors. For example, the following code compiles without errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// class type-casting
#include <iostream>
using namespace std;

class Dummy {
double i,j;
};

class Addition {
int x,y;
public:
Addition (int a, int b) { x=a; y=b; }
int result() { return x+y;}
};

int main () {
Dummy d;
Addition * padd;
padd = (Addition*) &d;
cout << padd->result();
return 0;
}



The program declares a pointer to Addition, but then it assigns to it a reference to an object of
another unrelated type using explicit type-casting:
padd = (Addition*) &d;


Unrestricted explicit type-casting allows to convert any pointer into any other pointer type,
independently of the types they point to. The subsequent call to member result will produce
either a run-time error or some other unexpected results.

In order to control these types of conversions between classes, we have four specific casting
operators:dynamic_cast, reinterpret_cast, static_cast and const_cast. Their format is to
follow the new type enclosed between angle-brackets (<>) and immediately after, the expression
to be converted between parentheses.

dynamic_cast <new_type> (expression)
reinterpret_cast <new_type> (expression)
static_cast <new_type> (expression)
const_cast <new_type> (expression)

The traditional type-casting equivalents to these expressions would be:

(new_type) expression
new_type (expression)

but each one with its own special characteristics:

You might also like