Title: Run Time Type Information, Binary files
1Run Time Type Information,Binary files
2RTTI why?
Shape
- Problem
- Up-casting works fine.
- Treating sub-class as base class
- Shape s new Circle()
- What about down-casting?
- might not be safe !
- correctness cannot be determined by the compiler.
- Circle c (Circle) s
Circle
Line
3RTTI
- RTTI Run Time Type Information
- Mechanisms for RTTI
- dynamic_cast operator
- typeid operator (and type_info class)
4The dynamic_cast operator
- dynamic_castltTgt(expression)
- Enables run-time type checking
- returns a valid pointer if expression is of
type T - Null pointer 0 otherwise
- Cast of a reference type throws an exception when
it fails (bad_cast)
5Dynamic_cast example
Shape s container.pop() Circle c
dynamic_castltCirclegt(s) if (c) // c is a
circle c-gtdoSomething() else handle()
//handle unexpected event
- Note the actual type of s is not mentioned!
- Can be any sub-class of Circle.
6Example
Shape
Line
Circle
- Shape s Circle c
- Line l ThinLine tl
- Shape arr4 s, c, l, tl
- for ( int i0 ilt3 i )
- Line pl dynamic_castltLinegt( arri )
- pc? cout ltlt cast ok\n
- cout ltlt cast fail\n
-
ThickLine
Output cast fail // Shape to Line cast fail //
Circle to Line cast ok // Line to Line cast
ok // ThickLine to Line
7dynamic_cast - more
- dynamic_castltTgt(expression)
- Note
- Used only on pointer or reference types.
- Can be used for up-cast, down-cast
- and also for cross-cast (out of this scope)
- Only for types with virtual-functions
(Polymorphic types) - These object have a space for the information
about the type the virtual function table
8dyn_cast only for polymorphics
class Date public Time // Time has no
virtual functions
class Circle public Shape virtual void
draw()
- void foo(Shape s, Time t)
- Circle c dynamic_castltCirclegt( s ) //ok
- Date date dynamic_castltDategt( t ) //error
9Static Cast
- Used when the compiler cannot assume anything
about the memory pointed to by a void. This
implies that dynamic cast cannot cast from a
void. For that, a static cast is needed. - R f(void p)
- S ps static_castltSgt (p)
- //trust the programmer.
10RTTI typeid operator
- Obtains info about an object/expression
- usage typeid( obj ) (like sizeof)
- ExampleDog dCat ccout ltlt d is a ltlt
typeid(d).name() ltlt , ltlt c is a ltlt
typeid(c).name() ltltendl - Output d is a Dog, c is a Cat
- typeid() returns a reference to a standard
library type called type_info defined in
lttype-infogt.
11type_info class
- typeid(expression) returns an object of the
type_info class. - e.g. type_info ti typeid(d)
- class type_info
- public
- bool operator( type_info const)const
- char const name() const
-
- private
- type_info( type_info const) //prevent copying
- type_info operator( type_info const )
12RTTI misuse
void rotate( shape const s ) if (typeid(s)
typeid(Circle) ) //do nothing else if
(typeid(s) typeid(Triangle) ) //rotate
Triangle else if (typeid(s)
typeid(Rectangle) ) //rotate Rectangle
- Use virtual functions (polymorphism) when you
can ! - VERY common misuse of RTTI
13Binary files
14Leading example image files
- Images are stored as matrices of numbers
(Pixels). - Here, we deal with gray-scale images (black and
white) - 8 bits per pixel
- i.e. each pixel between 0 and 255
- 0 is white, 255 is black, others are gray
-
255
0
0
0
255
100
15storing images
- How can we store images on files?
- For each image we want to store
- width //integer
- height //integer
- number of bits per pixel //short
- the pixels //matrix of integers
- Requirements read/write easily, save space,
save computation, etc.
16storing images
- First try text files (like you did so far)
- cons
- long
- needs parsing (e.g. atoi for numbers)
- pros
- readable by humans
- easy to edit
- but who handles images like that?
myImg.txt
width 3 height 2 bits_per_pixel 8 255, 0,
0 0, 255, 100
17Binary files
- Better solution Binary files
- Save the data the way the computer holds it
- pros
- Smaller
- No parsing (faster)
- Widely used !
- For example BMP files, other data
- cons
- hard to read for humans
- Machine dependant
18Images as binary files
width 2 bytes 0,,0,0,1,1
3
2
8
255
height 2 bytes 0,,0,0,1,0
0
0
0
255
100
19Images as binary files
3
2
8
255
bits per pixel 1 byte 0,0,0,0,1,0,0,0
0
0
0
255
100
20Images as binary files
pixel 1 byte 1,1,1,1,1,1,1,1
3
2
8
255
0
0
pixel 1 byte 0,0,0,0,0,0,0,0
0
255
100
21Binary files
3
2
16
255
bits per pixel now value 16
0
0
255
0
0
22Example writing a binary file
Pixel pixs30 int width 100, height
150 short bitsPerPix 16
ostream write( char const str, streamsize n)
23Learn by yourselvesc casting(see slides
ahead andStroustrup 15.4 and more)
24c style casting
- At the beginning c supported two styles of
casts - (typename)expression
- typename(expression)
- Instead there are now four new casting operators
- static_castlttypegt(expression)
- const_castlttypegt(expression)
- reinterpret_castlttypegt(expression)
- dynamic_castlttypegt(expression)
25The 'static_cast operator
static_castlttypegt(expression)
- Works where implicit conversion exists
- standard or user-defined conversion
- up-casts
- Safer that old-style casts
- e.g. wont cast int to float
- Failure causes a compiler error
- No dynamic checking is done!
- int i static_castltintgt(12.45)
-
26The const_cast'operator
const_castlttypegt(expression)
- Is used to remove (or add) const-ness
- void g(C cp)
- void f(C const cp)
- g(const_castltC gt(cp))
-
- Usually, you should design your
variables/methods such that you wont have to use
const_cast. - Failure causes compiler errors
27reinterpret_cast'operator
reinterpret_castlttypegt(expression)
- Is used to reinterpret byte patterns.
- double d(10.2)
- char dBytes
- reinterpret_castltchar gt(d)
- Circumvents the type checking of c.
- Very implementation-dependent.
- Rarely used.
- Very dangerous !