Składnia C++ Programowanie Obiektowe, część 3 Mateusz Cicheński
Rzutowanie Implicit/explicit cast Reinterpret cast Static cast Z wykorzystaniem Run-time Type Information Obsługa wyjątków Czym jest wyjątek w C++ Konstrukcja try/catch Zagnieżdżanie obsługi wyjątków Specyfikowanie rzucanych wyjątków Klasa exception Unexpected & terminate handler Plan zajęć
#include <iostream> class A { float x; ; class B { int y; void function() { cout << y << endl; ; float a = 4.5f; int b = a; //b = 4 int c = (int) a; //c = 4 int d = int (a); //d = 4 A a; B* ptr = (B*) &a; //one can convert a pointer to any pointer! ptr->function(); //either runtime error or unexpected result! Implicit/explicit cast
#include <iostream> class A { float x; virtual void ~A() { ; class B { int y; void function() { cout << y << endl; ; A a; B* ptr = reinterpret_cast<b*>(&a); //ptr!= NULL, possible runtime errors Reinterpret cast
#include <iostream> class A { float x; virtual void ~A() { ; class B : public A { int y; void function() { cout << y << endl; ; A a; B* ptr = static_cast<b*>(&a); //ptr!= NULL, possible runtime errors Static cast
#include <iostream> class A { float x; virtual void ~A() { ; class B { int y; void function() { cout << y << endl; ; A a; B* ptr = dynamic_cast<b*>(&a); if (ptr!= NULL) ptr->function(); //ptr == NULL Przy pomocy RTTI
#include <iostream> class A { float x; virtual void ~A() { ; class B : public A { int y; void function() { cout << y << endl; ; A a; B* ptr = dynamic_cast<b*>(&a); //ptr == NULL B b; A* aptr = &b; B* ptr2 = dynamic_cast<b*>(aptr); //ptr2!= NULL Przy pomocy RTTI c.d.
#include <iostream> #include <typeinfo> class A { ; class B : public A { ; virtual void ~A() { A a; B b; A* ptr = &b; A& ref = b; cout << typeid(a).name() << endl; //1A cout << typeid(b).name() << endl; //1B cout << typeid(ptr).name() << endl; //P1A cout << typeid(*ptr).name() << endl; //1B cout << typeid(ref).name() << endl; //1B Przy pomocy RTTI c.d.
Jętka rząd owadów uskrzydlonych, okres życia larw może trwać nawet do kilku lat, postacie dorosłe żyją bardzo krótko Dowolny obiekt! Wyjętka pochodna jętki Czym jest wyjętka w C++ wyjątek
#include <iostream> cout << 1; try { cout << 2; throw 3; cout << 4; catch (int e) { cout << e; cout << 5; $./main 1235 output Przykład obsługi wyjątku
try { //catch int exceptions catch (int e) { //catch char exceptions catch (char e) { //catch any unhandled exceptions catch ( ) { Konstrukcja try/catch
try { try { catch (int e) { throw; catch ( ) { throw; catch (char e) { catch ( ) { Zagnieżdżona obsługa wyjątków
//can throw any exception void function1(); //can throw only char exceptions void function2() throw (char); //cannot throw any exception void function3() throw (); Specyfikowanie rzucanych wyjątków
class exception { exception () throw(); exception (const exception&) throw(); exception& operator= (const exception&) throw(); virtual ~exception() throw(); virtual const char* what() const throw(); #include <iostream> #include <exception> exception try { int* arr = new int[1000000000]; catch (exception& e) { cout << e.what() << endl; //std::bad_alloc Klasa exception
void myunexpected() { throw; void myterminate() { //e.g. prevent termination set_unexpected(myunexpected); set_terminate(myterminate); Unexpected & terminate
#include <iostream> void myunexpected() { throw; void function() throw (int, bad_exception) { throw 3.14159265359f; set_unexpected(myunexpected); try { function(); catch (exception& e) { cout << e.what() << endl; //std::bad_exception cout << 5; Klasa bad_exception
Default: unexpected == terminate == abort(); void function() throw (int) { throw 3.14159265359f; => call unexpected => abort() void function() throw (int, bad_exception) { throw 3.14159265359f; => bad_exception thrown void myunexpected() { throw 3; set_unexpected(myunexpected); void function() throw (int) { throw 3.14159265359f; => OK., myunexpected will throw int instead of float Domyślna obsługa unexpected/terminate