c++ style casting At the beginning c++ supported two styles of casts:

Slides:



Advertisements
Similar presentations
Type Conversion. C provides two methods of changing the type of an expression: Type conversion (done implicitly) Cast expressions (done explicitly) Examples.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Traps and pitfalls for VC6, ATL, C++ May be obvious ? May be not ? Mark Bartosik.
CS102 Data Types in Java CS 102 Java’s Central Casting.
. Plab – Tirgul 11 RTTI, Binary files. RTTI – why? Problem: u Up-casting works fine.  Treating sub-class as base class Shape * s = new Circle(); u What.
Run-time type information (RTTI) and casts Consider classes for components and windows: class Component {... virtual void draw() {} }; class Window: public.
Run Time Type Information, Binary files. RTTI – why? Problem: Up-casting works fine. –Treating sub-class as base class Shape * s = new Circle(); What.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Templates ~ their instantiation and specialization.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
C++ Loose ends from last time. Variable initialization You can do the usual things int x; x = 10; int y = 20; And you can do an unusual thing int x(10);
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
Mixing integer and floating point numbers in an arithmetic operation.
Object Oriented Programming (OOP) Lecture No. 10.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Computer Graphics Basic Maths for Graphics in C++ CO2409 Computer Graphics Week 4.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Inheritance and Composition Reusing the code and functionality Unit - 04.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Data Types Always data types will decide which type of information we are storing into variables In C programming language we are having 3 types of basic.
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
Object Oriented Programming Lecture 2: BallWorld.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
C++ Lesson 1.
Computer Graphics Basic Maths for Graphics in C++
The C++ Data Types Fundamental Data Types
Introduction to C++ (Extensions to C)
Inheritance.
7. Inheritance and Polymorphism
Writing and Reading Raw Data
Object-Oriented Programming (OOP) Lecture No. 21
Advanced Program Design with C++
Inheritance.
CSE 374 Programming Concepts & Tools
CISC/CMPE320 - Prof. McLeod
Object-Oriented Programming (OOP) Lecture No. 32
Chapter 2.
Programming with ANSI C ++
Conversions of the type of the value of an expression
Explicit and Implicit Type Changes
Data Types and Expressions
null, true, and false are also reserved.
Arrays and Classes Programming in C++ Fall 2008
CMSC 202 Lesson 22 Templates I.
Pointers.
Compiler design.
Dynamic Memory A whole heap of fun….
Default Arguments.
Lecture 5 Inheritance, Polymorphism, Object Memory Model, The Visitor Pattern …and gazillion other tidbits!
C++ Inheritance II, Casting CSE 333 Summer 2018
Pointer Operations.
Dynamic Memory A whole heap of fun….
Templates I CMSC 202.
Java Programming Language
Java’s Central Casting
C Data Types and Variable
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
CISC/CMPE320 - Prof. McLeod
Subtype Substitution Principle
Casting Converting types.
Class rational part2.
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Chapter 3 The New Math.
Presentation transcript:

c++ style casting At the beginning c++ supported two styles of casts: (typename)expression typename(expression) Instead there are now four new casting operators: static_cast<type>(expression) const_cast<type>(expression) reinterpret_cast<type>(expression) dynamic_cast<type>(expression)

The 'static_cast operator static_cast<type>(expression) Works where implicit conversion exists standard or user-defined conversion up-casts Safer that “old-style” casts e.g. won’t cast int* to float* Failure causes a compiler error No dynamic checking is done! int i = static_cast<int>(12.45);

The ‘const_cast'operator const_cast<type>(expression) Is used to remove (or add) const-ness: void g(C * cp); void f(C const* cp) { g(const_cast<C *>(cp)); } Usually, you should design your variables/methods such that you won’t have to use const_cast. Failure causes compiler errors

‘reinterpret_cast'operator reinterpret_cast<type>(expression) Is used to reinterpret byte patterns. double d(10.2); char* dBytes = reinterpret_cast<char *>(&d); Circumvents the type checking of c++. Very implementation-dependent. Rarely used. Very dangerous !