CS246 Lecture Notes - Lecture 22: Virtual Function, Downcasting, In C

20 views2 pages

Document Summary

C++ different syntax which stands out. Different kinds with vaarying power static_cast (sensible casts) int m=9; int n=2; cout << m/n << endl; // prints 4 cout << static_cast(m)/n << endl; // prints 4. 5. Book *bp = new textbook( ); You need to be a 100% sure!! (unchecked cast) Requirement: there needs to be an is a relationship. If bp does not point to a textbook, the cast fails at runtime leads to undefined behaviour const_cast. Used to add/remove the const property void g(int *p){ } void f(const int *q){ // we cannot use q to change the value it is pointing to g(q); // won"t compile g(const_cast(q)); // will compile. If g does not use p to change the value pointed to, this code still has defined behaviour. Since g made no such promise, it could change teh value poitned to by p, which could lead to undefined behaviour reinterpret_cast.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers