These new operators are intended to remove some of the holes in the C type system introduced by the old C-style casts.

Similar documents
Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

Casting in C++ (intermediate level)

Tokens, Expressions and Control Structures

OBJECT ORIENTED PROGRAMMING USING C++

C++ Programming: Polymorphism

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

Inclusion Polymorphism

C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015

C++ Casts and Run-Time Type Identification

CS11 Advanced C++ Fall Lecture 7

COEN244: Polymorphism

Explicit Conversion Operator Draft Working Paper

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Where do we stand on inheritance?

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

Dr. Md. Humayun Kabir CSE Department, BUET

Operator Overloading. Gary Powell, Doug Gregor, Jaakko Jȧ. rvi

Laboratorio di Tecnologie dell'informazione

CS3157: Advanced Programming. Outline

G52CPP C++ Programming Lecture 15

W3101: Programming Languages C++ Ramana Isukapalli

CPSC 427: Object-Oriented Programming

Variables. Data Types.

A Taxonomy of Expression Value Categories

C++ Coding Standards. 101 Rules, Guidelines, and Best Practices. Herb Sutter Andrei Alexandrescu. Boston. 'Y.'YAddison-Wesley

The Four Polymorphisms in C++ Subtype Polymorphism (Runtime Polymorphism) Polymorphism in C

Increases Program Structure which results in greater reliability. Polymorphism

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

Assignment 1: grid. Due November 20, 11:59 PM Introduction

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

C++ without Classes. CMSC433, Fall 2001 Programming Language Technology and Paradigms. More C++ without Classes. Project 1. new/delete.

Modernizing legacy C++ code

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Rvalue References as Funny Lvalues

Chapter 17 vector and Free Store. Bjarne Stroustrup

C++ Important Questions with Answers

1c) (iv) and (v) OR (v) only (hindsight showed that the question was more ambiguous than intended)

dewhurst_index.qxd 10/16/02 1:54 PM Page 309 Index

Topic 9: Type Checking

Topic 9: Type Checking

PIC 10A Objects/Classes

Introduction to Programming

A Generic Non-intrusive Smart Pointer Implementation

Java Primer 1: Types, Classes and Operators

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Type Inference auto for Note: Note:

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

TPF Users Group Spring 2005

const-correctness in C++

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

CS105 C++ Lecture 7. More on Classes, Inheritance

CSE 333 Lecture C++ final details, networks

Using Enum Structs as Bitfields

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Vector and Free Store (Pointers and Memory Allocation)

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Introduction to C++ Systems Programming

THE PROGRAMMING RESEARCH GROUP

Lecture 20: templates

C-style casts Static and Dynamic Casts reinterpret cast const cast PVM. Casts

QUIZ. What is wrong with this code that uses default arguments?

Types. What is a type?

Introduction to RTTI. Jonathan Hoyle Eastman Kodak 11/30/00

Programming for Engineers Iteration

CSE 12 Abstract Syntax Trees

Advanced C++ Topics. Alexander Warg, 2017

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

Basic Types, Variables, Literals, Constants

CS201- Introduction to Programming Current Quizzes

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

04-19 Discussion Notes

American National Standards Institute Reply to: Josee Lajoie

ECE 3574: Dynamic Polymorphism using Inheritance

C Language Programming

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Chapter 15 - C++ As A "Better C"

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Chapter 1 Getting Started

The Compositional C++ Language. Denition. Abstract. This document gives a concise denition of the syntax and semantics

Generalized Constant Expressions

Object-Oriented Principles and Practice / C++

Absolute C++ Walter Savitch

Ch. 3: The C in C++ - Continued -

Object-Oriented Principles and Practice / C++

CS OBJECT ORIENTED PROGRAMMING

I m sure you have been annoyed at least once by having to type out types like this:

CPSC 427: Object-Oriented Programming

C++ Inheritance II, Casting


static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Overload Resolution. Ansel Sermersheim & Barbara Geller Amsterdam C++ Group March 2019

Instantiation of Template class

Transcription:

asting in C++: Bringing Safety and Smartness to Your Programs of 10 10/5/2009 1:20 PM By G. Bowden Wise The new C++ standard is full of powerful additions to the language: templates, run-time type identification (RTTI), namespaces, and exceptions to name a few. Rather than talk about one of these ``major'' extensions, I will discuss one of the minor extensions: the new C++ casting operators. The C++ draft standard includes the following four casting operators: static_cast const_cast dynamic_cast, and reinterpret_cast. These new operators are intended to remove some of the holes in the C type system introduced by the old C-style casts. In this article we will learn about casting in general, discuss the problems with the old C-style cast, and take a look at the new C++ casting operators in detail. Why Cast? Casts are used to convert the type of an object, expression, function argument, or return value to that of another type. Some conversions are performed automatically by the compiler without intervention by the programmer. These conversions are called implicit conversions. The standard C++ conversions and user-defined conversions are performed implicitly by the compiler where needed. Other conversions must be explicitly specified by the programmer and are appropriately called explicit conversions. Standard conversions are used for integral promotions (e.g., enum to int), integral conversions (e.g., int to unsigned int), floating point conversions (e.g., float to double), floating-integral conversions (e.g., int to float), arithmetic conversions (e.g., converting operands to the type of the widest operand before evaluation), pointer conversions (e.g., derived class pointer to base class pointer), reference conversions (e.g., derived class reference to base class reference), and pointer-to-member conversions. You can provide a user-defined conversion from a class X to a class Y by providing a constructor for Y that takes an X as an argument: Y(const X& x) or by providing a class Y with a conversion operator: operator X() When a type is needed for an expression that cannot be obtained through an implicit conversion or when more than one standard conversion creates an ambiguous situation, the programmer must explicitly specify the target type of the conversion. In C, an expression, expr, of type S can be cast to another type T in one of the following ways. By

2 of 10 10/5/2009 1:20 PM using an explicit cast: (T) expr or by using a functional form: T(expr) We will refer to either of these constructs as the old C-style casts. The old C-style casts have several shortcomings. First, the syntax is the same for every casting operation. This means it is impossible for the compiler (or users) to tell the intended purpose of the cast. Is it a cast from a base class pointer to a derived class pointer? Does the cast remove the ``const-ness'' of the object? Or, is it a conversion of one type to a completely unrelated type? The truth is, it is impossible to tell from the syntax. As a result, this makes the cast harder to comprehend, not only by humans, but also by compilers which are unable to detect improper casts. Another problem is that the C-style casts are hard to find. Parentheses with an identifier between them are used all over C++ programs. There is no easy way to ``grep'' a source file and get a list of all the casts being performed. Perhaps the most serious problem with the old C-style cast is that it allows you to cast practically any type to any other type. Improper use of casts can lead to disastrous results. The old C-style casts have created a few holes in the C type system and have also been a souce of confusion for both programmers and compilers. Even in C++, the old C-style casts are retained for backwards compatibility. However, using the new C++ style casting operators will make your programs more readable, type-safe, less error-prone, and easier to maintain. The New C++ Casting Operators The new C++ casting operators are intended to provide a solution to the shortcomings of the old C-style casts by providing: Improved syntax. Casts have a clear, concise, although somewhat cumbersome syntax. This makes casts easier to understand, find, and maintain. Improved semantics. The intended meaning of a cast is no longer ambiguous. Knowing what the programmer intended the cast to do makes it possible for compilers to detect improper casting operations. Type-safe conversions. Allow some casts to be performed safely at run-time. This will enable programmers to check whether a particular cast is successful or not. C++ introduces four new casting operators: static_cast, to convert one type to another type; const_cast, to cast away the ``const-ness'' or ``volatile-ness'' of a type; dynamic_cast, for safe navigation of an inheritance hierarchy; and reinterpret_cast, to perform type conversions on un-related types. All of the casting operators have the same syntax and are used in a manner similar to templates. For example, to perform a static_cast of ptr to a type T we write: T* t = static_cast<t> (ptr); As we will soon see static cast is the most general and is intended as a replacement for most

3 of 10 10/5/2009 1:20 PM As we will soon see, static_cast is the most general and is intended as a replacement for most C-style casts. The other three forms are for specific circumstances to be discussed below. The static_cast Operator The static_cast operator takes the form static_cast<t> (expr) to convert the expression expr to type T. Such conversions rely on static (compile-time) type information. Subject to certain restrictions, you may use static_cast to convert a base class pointer to a derived class pointer, perform arithmetic conversions, convert an int to an enum, convert a reference of type X& to another reference of type Y&, convert an object of type X to an object of type Y, and convert a pointer-to-member to another pointer-to-member within the same class hierarchy. Internally, static_casts are used by the compiler to perform implicit type conversions such as the standard conversions and user-defined conversions. In general, a complete type can be converted to another type so long as some conversion sequence is provided by the language. The downcast of a base class pointer X* to a derived class pointer Y* can be done statically only if the conversion is unambiguous and X is not a virtual base class. Consider this class hierarchy: class BankAcct /*... */ class SavingsAcct : public BankAcct /*... */ Given a base class pointer, we can cast it to a derived class pointer: void f (BankAcct* acct) SavingsAcct* d1 = static_cast<savingsacct*>(acct); This is called a downcast. The static_cast operator allows you to perform safe downcasts for non-polymorphic classes. Note that static_cast relies on static (compile-time) type information and does not perform any run-time type checking. This means that if acct does, in fact, not refer to an actual SavingsAcct the result of the cast is undefined. Borland C++ 4.5, seemingly incorrectly, still performs the conversion, however. Your compiler mileage may vary. If you want to use run-time type information during conversion of polymorphic class types, use dynamic_cast. It is not possible to perform a downcast from a virtual base class using a static_cast; you must use a dynamic_cast. More generally, a static_cast may be used to perform the explicit inverse of the implicit standard conversions. A conversion from type S to T can only be done if the conversion from type T to S is an implicit conversion. Also, the ``const-ness'' of the original type, S, must be preserved. You cannot use static cast to change ``const-ness''; use const cast instead

4 of 10 10/5/2009 1:20 PM use static_cast to change const ness ; use const_cast instead. One of the more common uses of static_cast is to perform arithmetic conversions, such as from int to double. For example, to avoid the truncation in the following computation: int total = 500; int days = 9; double rate = total/days; We can write: double rate = static_cast<double>(total)/days; A static_cast may also be used to convert an integral type to an enumeration. Consider: enum fruit apple=0,orange,banana; int i 1 = 2; fruit f1 = static_cast<fruit> (i1); The conversion results in an enumeration with the same value as the integral type provided the integral value is within the range of the enumeration. The conversion of an integral value that is not within the range of the enumeration is undefined. You may also use static_cast to convert any expression to a void, in which case the value of the expression is discarded. One interesting side effect of the old C-style casts, was to gain access to a private base class of a derived class. Consider this hierarchy: class Base public: Base() : _data(999) int Data() const return _data; private: int _data; ; class Derived : private Base public: Derived () : Base() ; Derived* d1 = new Derived; Normally, you should not be able to access Data() through the pointer d1. However, using an old C-style cast, we can:

5 of 10 10/5/2009 1:20 PM Base* b1 = (Base*) d1; int i = b1->data(); // works! The good news is that if you attempt to use static_cast: Base* b1 = static_cast<base*>(d1); the compiler will correctly report that Base is inaccessible because it is a private base class. Another unfortunate hole created in the type system by the old C-style casts results with incomplete types. Consider: class X; // incomplete class Y; // incomplete The old C-style casts, let us cast from one incomplete type to another! Here is an example: void f(x* x) Y* y = (Y*) x; // works! Thankfully, this hole has also been plugged by static_cast: void f(x* x) Y* y = static_cast<y*> x; // fails The const_cast Operator The const_cast operator takes the form const_cast<t> (expr) and is used to add or remove the ``const-ness'' or ``volatile-ness'' from a type. Consider a function, f, which takes a non-const argument: double f(double& d); However, we wish to call f from another function g: void g (const double& d) val = f(d); Since d is const and should not be modified, the compiler will complain because f may potentially modify its value. To get around this dilemma, we can use a const_cast: void g (const double& d)

6 of 10 10/5/2009 1:20 PM g ( ) val = f(const_cast<double&>(d)); which strips away the ``const-ness'' of d before passing it to f. Another scenario where const_cast is useful is inside const functions. Remember that when you make a member function const, you are telling your users (and the compiler) that calling this function will not change the value of the object. However, in some cases, we find that it is sometimes still necessary to change the value of some internal data members inside a function that is const. For example, consider class B: class B public: B() ~B() void f() const; private: int _count; ; Suppose that, f(), which is declared to be const, must modify _count whenever it is called: void B::f() const _count += 1; The compiler will not allow _count to be changed because the function is const. Just how does the compiler perform this magic? It turns out that the type of the internal this pointer helps the compiler perform this check. Every non-static member function of a class C has a this pointer. For non-const member functions of class C, this has type C * const This means that this is a constant pointer. In other words, you cannot change what the pointer this points to, after all, that would be disastrous, wouldn't it? However, you can still change what ever this points to (i.e., you can change data members of class C). For const member functions of class C, this has a type of const C * const Not only is this a constant pointer but also what is pointed to is constant. So the data members of C may not be changed through the this pointer. This is how the compiler ensures that you do not modify data members inside t functions

7 of 10 10/5/2009 1:20 PM modify data members inside const functions. Examining the member function B::f again, the statement _count is actually interpreted as this->_count. But since this has type const B * const, it cannot be used to change the value of _count so the compiler reports an error. We can, however, use const_cast to cast away the ``const-ness'' of this: void B::f() const B* const localthis = const_cast<b* const>(this); localthis->_count += 1; Actually, you should not be casting away the ``const-ness'' of this using const_cast. C++ now has the keyword mutable for those data members whose value may be changed by const functions. By declaring _count as: mutable int _count; We can use the original implementation of B::f without casting away the ``const-ness'' of this. const_cast can also be used to strip away the ``volatile-ness'' of an object in a similar manner. You cannot use const_cast for any other types of casts, such as casting a base class pointer to a derived class pointer. If you do so, the compiler will report an error. The dynamic_cast Operator The dynamic_cast operator takes the form dynamic_cast<t> (expr) and can be used only for pointer or reference types to navigate a class hierarchy. The dynamic_cast operator can be used to cast from a derived class pointer to a base class pointer, cast a derived class pointer to another derived (sibling) class pointer, or cast a base class pointer to a derived class pointer. Each of these conversions may also be applied to references. In addition, any pointer may also be cast to a void*. The dynamic_cast operator is actually part of C++'s run-time type information, or RTTI, sub-system. As such, it has been provided for use with polymorphic classes -- those classes which have at least one virtual function. Use static_cast to perform conversions between non-polymorphic classes. All of the derived-to-base conversions are performed using the static (compile-time) type information. These conversions may, therefore, be performed on both non-polymorphic and polymorphic types. These conversions will produce the same result if they are converted using a static_cast. These conversions are fairly straightforward so we won't discuss them further. Conversions down the hierarchy from base to derived, or across a class hierarchy, rely on run-time type information and can only be performed on polymorphic types. Such conversions can now be performed safely since dynamic_cast will indicate whether the conversion is successful. When

8 of 10 10/5/2009 1:20 PM performing a dynamic_cast on a pointer, a null pointer is returned when the cast is unsuccessful. When a reference is being cast, a Bad_cast exception is thrown. Let's look at the power of run-time type conversions by revisiting the bank account hierarchy introduced above with static_cast. Recall that when acct does not actually point to a SavingsAcct object, the result of the static_cast is undefined. Since BankAcct has at least one virtual function, it is a polymorphic class. We can use a dynamic_cast instead to check that the cast was successful: void f (BankAcct* acct) SavingsAcct* d1 = dynamic_cast<savingsacct*>(acct); if (d1) // d1 is a savings account Let's expand our bank account hierarchy to include a few more types of accounts, such as a checking account and a money market account. Let's suppose we also want to extend the functionality so that we can credit the interest for all savings and money market accounts in our database. Suppose further that BankAcct is part of a vendor library; we are not able to add any new members functions to BankAcct since we do not have the source code. Clearly, the best way to incorporate the needed functionality would be to add a virtual function, creditinterest() to the base class, BankAcct. But since we are not able to modify BankAcct, we are unable to do this. Instead, we can employ a dynamic_cast to help us. We add the method creditinterest() to both SavingsAcct and MMAcct classes. The resulting class hierarchy looks like: class BankAcct /*... */ class SavingsAcct : public BankAcct public: //... void computeinterest(); class MMAcct : public BankAcct public: //... void computeinterest(); We can now compute interest for an array of BankAcct*s: void DoInterest (BankAcct* a[]

asting in C++: Bringing Safety and Smartness to Your Programs of 10 10/5/2009 1:20 PM void DoInterest (BankAcct* a[], int num_accts) for (int i = 0; i < num_accts; i++) // Check for savings SavingsAcct* sa = dynamic_cast<savingsacct*>(accts[i]); if (sa) sa->creditinterest(); MMAcct* mm = dynamic_cast<mmacct*>(accts[i]); if (mm) mm->creditinterest(); A dynamic_cast will return a null pointer if the cast is not successful. So only if the pointer is of type SavingsAcct* or MMAcct* is interest credited. dynamic_cast allows you to perform safe type conversions and lets your programs take appropriate actions when such casts fail. When a pointer is converted to a void*, the resulting object points to the most derived object in the class hierarchy. This enables the object to be seen as raw memory. Meyers [3] demonstrates how a cast to void* can be used to determine if a particular object is on the heap. The reinterpret_cast Operator The reinterpret_cast operator takes the form reinterpret_cast<t> (expr) and is used to perform conversions between two unrelated types. The result of the conversion is usually implementation dependent and, therefore, not likely to be portable. You should use this type of cast only when absolutely necessary. A reinterpret_cast can also be used to convert a pointer to an integral type. If the integral type is then converted back to the same pointer type, the result will be the same value as the original pointer. Meyers [3] shows how reinterpret_casts can be used to cast between function pointer types. Summary The new C++ cast operators enable you to develop programs which are easier to maintain and

asting in C++: Bringing Safety and Smartness to Your Programs 0 of 10 10/5/2009 1:20 PM understand, and perform some conversions safely. Casts should not be taken lightly. As you convert your old C-style casts to use the new C++ casting operators, ask yourself if a cast is really needed there. It may be that you are using a class hierarchy in a way not originally intended or that you may be able to do the same thing with virtual functions.