1 / 18

TYPE CONVERSION

TYPE CONVERSION. short a=2000; int b; b=a;. Type is implicitly converted here s izeof (short)==2 but sizeof ( int )==4 // This is a case of “type promotion” Easily accommodated, Guaranteed to work correctly but…. http://www.cplusplus.com/doc/tutorial/typecasting/. TYPE CONVERSION.

tarpley
Download Presentation

TYPE CONVERSION

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TYPE CONVERSION short a=2000; int b; b=a; Type is implicitly converted here sizeof(short)==2 but sizeof(int)==4 // This is a case of “type promotion” Easily accommodated, Guaranteed to work correctly but…. http://www.cplusplus.com/doc/tutorial/typecasting/

  2. TYPE CONVERSION short a=2000; int b; b=a; Type is implicitly converted here sizeof(short)==2 but sizeof(int)==4 Easily accommodated, but…. int a=2000000……………………….00001; short b; b=a; http://www.cplusplus.com/doc/tutorial/typecasting/

  3. TYPE CONVERSION int a=2000000……………………….001111; short b; b=a; Truncation will happen above Result may not be accurate Compiler expects you to know what you are doing http://www.cplusplus.com/doc/tutorial/typecasting/

  4. A USEFUL TYPE DEMOTION int a; double b=32.43; a=b; // a == 32, only integer part b=a; // b == 32.0 http://www.cplusplus.com/doc/tutorial/typecasting/

  5. SOME RULES OF TYPE CONVERSION • For conversion from a floating-point type to an integer type, the value is truncated (the decimal part is removed). • If the result lies outside the range of representable values by the type, the conversion causes undefined behavior. • Some conversions may be implementation-specific(and may not be portable). • Null pointers can be converted to pointers of any type • Any type of pointers may be converted to voidpointers http://www.cplusplus.com/doc/tutorial/typecasting/

  6. Try this: int main () { int * foo; *foo=5; // foo = nullptr; // keep and remove commenting, both *foo = 5.5; // or, *foo = ‘j’; cout << *foo; }

  7. TYPE CASTING double x = 10.3; int y; y = int (x); // functional notation y = (int) x; // c-like cast notation http://www.cplusplus.com/doc/tutorial/typecasting/

  8. CLASS TYPE CASTING // 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(); } http://www.cplusplus.com/doc/tutorial/typecasting/

  9. IMPLICIT TYPE CONVERSION // implicit conversion of classes: #include <iostream> using namespace std; class A {}; class B { public: B (const A& x) {} // conversion from A within constructor B& operator= (const A& x) {return *this;} // conversion from A for assignment operator A() {return A();} // conversion to A as type-cast operator }; int main () { A foo; B bar = foo; // calls constructor bar = foo; // calls assignment foo = bar; // calls type-cast operator, Try foo = A(bar) } http://www.cplusplus.com/doc/tutorial/typecasting/

  10. IMPLICIT TYPE CONVERSION In the world of classes, implicit conversions can be controlled by means of three member functions: • Single-argument constructors: allow implicit conversion from a particular type to initialize an object. • Assignment operator: allow implicit conversion from a particular type on assignments. • Type-cast operator: allow implicit conversion to a particular type. http://www.cplusplus.com/doc/tutorial/typecasting/

  11. IMPLICIT TYPE CONVERSION // implicit conversion of classes: #include <iostream> using namespace std; class A {}; Class B { public: B (const A& x) {} // conversion from A (constructor) B& operator= (const A& x) {return *this;} // conversion from A (assignment) operator A() {return A();} // conversion to A (type-cast operator) }; void fn (B arg) {} int main () { A foo; B bar = foo; // calls constructor, like bar(foo) bar = foo; // calls assignment foo = bar;// calls type-cast operator fn (foo); // works by implicit conversion to B type }

  12. IMPLICIT CONVERSION PREVENTED BY “EXPLICIT” CONVERSION // explicit: #include <iostream> using namespace std; class A {}; class B { public: explicit B (const A& x) {} B& operator= (const A& x) {return *this;} operator A() {return A();} }; void fn (B x) {} int main () { A foo; // B bar = foo; // assignment-like op is not allowed for explicit marked constructor B bar (foo); // however, the constructor itself is ok bar = foo; foo = bar; // fn (foo); // not allowed for explicit constructor!! fn (bar); }

  13. Dynamic Cast // dynamic_cast #include <iostream> #include <exception> using namespace std; class Base { virtual void dummy() {} }; class Derived: public Base { int a; }; int main () { try { Base * pba = new Derived; Base * pbb = new Base; Derived * pd; pd = dynamic_cast<Derived*>(pba); // no problem, casting worked if (pd==0) cout << "Null pointer on first type-cast.\n"; pd = dynamic_cast<Derived*>(pbb); // Base pointed by pbb is not a complete object of Derived type if (pd==0) cout << "Null pointer on second type-cast.\n"; } catch (exception& e) {cout << "Exception: " << e.what();} // try-catch has no significance here return 0; } dynamic_cast can also downcast (convert from pointer-to-base to pointer-to-derived) polymorphic classes (those with virtual members) if -and only if- the pointed object is a valid complete object of the target type. Null pointer on second type-cast.

  14. class Fruit { public: virtual ~Fruit() { } }; class Banana : public Fruit { }; int main(int argc, char **argv) { Fruit * fp = new Banana; Banana * bp = dynamic_cast<Banana*>(fp); if (bp != NULL) { cout << "Banana detected!\n"; } } https://cboard.cprogramming.com/cplusplus-programming/90608-why-dynamic_cast-needs-class-has-virtual-methods.html

  15. Static Cast No checks are performed during runtime to guarantee that the object being converted is in fact a full object of the destination type (unlike in dynamic_cast). Therefore, it is up to the programmer to ensure that the conversion is safe. On the other hand, it does not incur the overhead of the type-safety checks of dynamic_cast. class Base {}; class D erived: public Base {}; Base * a = new Base; Derived * b = static_cast<Derived*>(a); // may lead to run time error

  16. Const Cast in order to pass a const pointer to a function that expects a non-const argument: // const_cast #include <iostream> using namespace std; void print (char * str) { // try, str = ‘j’; cout << str << '\n'; // try, *str instead of str here } int main () { const char * c = "sample text"; print ( const_cast<char *> (c) ); } sample text Note, that removing the constness of a pointed object to actually write to it causes undefined behavior.

  17. Const Cast // const_cast #include <iostream> using namespace std; void print (char * str) // try, (const char * str) { cout << str << '\n'; } int main () { const char * c = "sample text"; print ( const_cast<char *> (c) ); // try removing const_cast }

  18. QUERYING TYPE OF OBJECT: TYPEID // typeid, like we have sizeof() #include <iostream> using namespace std; #include <typeinfo> int main () { int * a, b; a=0; b=0; if (typeid(a) != typeid(b)) { cout << "a and b are of different types:\n"; cout << "a is: " << typeid(a).name() << '\n'; cout << "b is: " << typeid(b).name() << '\n'; } return 0; }

More Related