Download presentation
Presentation is loading. Please wait.
1
. Plab – Tirgul 11 RTTI, Binary files
2
RTTI – why? Problem: u Up-casting works fine. Treating sub-class as base class Shape * s = new Circle(); u What about down-casting? might not be safe ! correctness cannot be determined by the compiler. Circle * c = (Circle*) s; Shape Line Circle
3
RTTI RTTI – Run Time Type Information Mechanisms for RTTI: 1. dynamic_cast operator 2. typeid operator (and type_info class)
4
The ‘dynamic_cast‘ operator dynamic_cast (expression) u Enables run-time type checking: returns a valid pointer if “expression” is of type “T” NULL otherwise u Cast of a reference type throws an exception when it fails (“bad_cast”)
5
Dynamic_cast : example Shape* s = container.pop(); Circle* c = dynamic_cast (s); if ( c != NULL ) { // c is a circle c->doSomething(); else handle(); //handle unexpected event Note: the actual type of “ s ” is not mentioned! Can be any sub-class of Circle.
6
Dynamic_cast : example Shape s; Circle c; Line l; ThinLine tl; Shape* arr[4] = {&s, &c, &l, &tl}; for ( int i=0; i<3; i++ ) { Line * pl = dynamic_cast ( arr[i] ); pc == NULL? cout << “cast ok\n” : cout << “cast fail\n”; } Shape LineCircle Output: cast fail // Shape to Line cast fail // Circle to Line cast ok // Line to Line cast ok // ThickLine to Line ThickLine
7
dynamic_cast - more dynamic_cast (expression) Note: u Used only on pointer or reference types. u Can be used for up-cast, down-cast u and also for cross-cast (out of this scope) u Only for types with virtual-functions (“Polymorphic types”) u These object have a space for the information about the type: the virtual function table
8
dyn_cast: only for polymorphics void foo(Shape * s, Time * t) { Circle * c = dynamic_cast ( s ); //ok Date * date = dynamic_cast ( t ); //error } class Circle : public Shape { virtual void draw(); … } class Date : public Time { …// Time has no virtual functions }
9
RTTI : typeid operator Obtains info about an object/expression usage: typeid( obj ) (like “sizeof”) Example: Dog d; Cat c; cout << “d is a “ << typeid(d).name() << “,” << “c is a “ << typeid(c).name() <<endl; Output: d is a Dog, c is a Cat
10
type_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& ); }
11
RTTI 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 (i.e. polymorphism) when you can ! VERY common misuse of RTTI
12
. Binary files
13
Leading example: image files u Images are stored as matrices of numbers (Pixels). u Here, we deal with gray-scale images (“black and white”) u 8 bits per pixel i.e. each pixel between 0 and 255 u 0 is white, 255 is black, others are gray 255 0 0 0 100
14
storing images u How can we store images on files? u For each image we want to store: width//integer height//integer number of bits per pixel//short the pixels//matrix of integers u Requirements: read/write easily, save space, save computation, etc.
15
storing images First try: text files (like you did so far) width = 3 height = 2 bits_per_pixel = 8 255, 0, 0 0,255,100 “myImg.txt” cons: u long u needs parsing (e.g. atoi for numbers) pros: u readable by humans u easy to edit u but who handles images like that?
16
Binary files Better solution: Binary files u Save the data the way the computer holds it pros: u Smaller u No parsing (faster) u Widely used ! u For example: BMP files, other data cons: u hard to read for humans u Machine dependant
17
Images as binary files 3 2 8255 00 0 100 width 2 bytes [0,…,0,0,1,1] height 2 bytes [0,…,0,0,1,0]
18
Images as binary files 3 2 8255 00 0 100 bits per pixel 1 byte [0,0,0,0,1,0,0,0]
19
Images as binary files 3 2 8255 00 0 100 pixel 1 byte [1,1,1,1,1,1,1,1] pixel 1 byte [0,0,0,0,0,0,0,0]
20
Binary files 3 2 16 255 0 0 0 0 bits per pixel now value 16
21
Example: writing a binary file Pixel pixs[30]; int width = 100, height = 150; short bitsPerPix = 16; binary open for writing ofstream outfile; outfile.open(“pic.raw", ios::bin | ios::out); outfile.write(&width, sizeof(int)); outfile.write(&height, sizeof(int)); outfile.write(&bitsPerPix, sizeof(short)); outfile.write(pixs, 30*sizeof(Pixel)); outfile.close(); ostream& write( char const* str, streamsize n);
22
. Learn by yourselves: c++ casting (see slides ahead and Stroustrup 15.4 and more)
23
c++ style casting u At the beginning c++ supported two styles of casts: (typename)expression typename(expression) u Instead there are now four new casting operators: static_cast (expression) const_cast (expression) reinterpret_cast (expression) dynamic_cast (expression)
24
The 'static_cast'operator u Works where implicit conversion exists standard or user-defined conversion up-casts u Safer that “old-style” casts e.g. won’t cast int* to float* u Failure causes a compiler error No dynamic checking is done! int i = static_cast (12.45); static_cast (expression)
25
The ‘const_cast'operator Is used to remove (or add) const -ness: void g(C * cp); void f(C const* cp) { g(const_cast (cp)); } Usually, you should design your variables/methods such that you won’t have to use const_cast. u Failure causes compiler errors const_cast (expression)
26
‘reinterpret_cast'operator u Is used to reinterpret byte patterns. double d(10.2); char* dBytes = reinterpret_cast (&d); u Circumvents the type checking of c++. u Very implementation-dependent. u Rarely used. ! Very dangerous ! reinterpret_cast (expression)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.