CS11 Advanced C++ Fall Lecture 7

Similar documents
CS11 Introduction to C++ Fall Lecture 6

OBJECT ORIENTED PROGRAMMING USING C++

ECE 3574: Dynamic Polymorphism using Inheritance

W3101: Programming Languages C++ Ramana Isukapalli

CS11 Advanced C++ Fall Lecture 3

Where do we stand on inheritance?

C++ Programming: Polymorphism

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

Tokens, Expressions and Control Structures

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

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

Laboratorio di Tecnologie dell'informazione

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

G52CPP C++ Programming Lecture 15

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

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

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

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

Lecture 20: templates

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

Short Notes of CS201

CS11 Introduction to C++ Fall Lecture 7

CS201 - Introduction to Programming Glossary By

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

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

Fast Introduction to Object Oriented Programming and C++

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

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

CS11 Advanced C++ Spring 2018 Lecture 2

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

CS3157: Advanced Programming. Outline

Chapter 17 vector and Free Store. Bjarne Stroustrup

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

CSE 333 Lecture C++ final details, networks

Programming, numerics and optimization

C++ Inheritance II, Casting

Variables. Data Types.

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

Introduction to C++ with content from

Sixth lecture; classes, objects, reference operator.

Dr. Md. Humayun Kabir CSE Department, BUET

Advanced C++ Programming Workshop (With C++11, C++14, C++17) & Design Patterns

COEN244: Class & function templates

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

Exercise 6.2 A generic container class

Basic Types, Variables, Literals, Constants

C++ Alexander Baltatzis

Operators. The Arrow Operator. The sizeof Operator

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

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

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

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

Overload Resolution. Ansel Sermersheim & Barbara Geller ACCU / C++ June 2018

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

Modernizing legacy C++ code

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

COMP322 - Introduction to C++

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

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

Important From Last Time

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

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

Increases Program Structure which results in greater reliability. Polymorphism

Chapter 17 vector and Free Store

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CPSC 427: Object-Oriented Programming

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Fibonacci in Lisp. Computer Programming: Skills & Concepts (CP1) Programming Languages. Varieties of Programing Language

C++ Inheritance II, Casting

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

CSE 12 Week Eight, Lecture One

C++ Inheritance and Encapsulation

Lecture 10: building large projects, beginning C++, C++ and structs

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

C++ Important Questions with Answers

CS 251 Intermediate Programming Methods and Classes

Vector and Free Store (Pointers and Memory Allocation)

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

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

CPSC 427: Object-Oriented Programming

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types.

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

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

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Topics. bool and string types input/output library functions comments memory allocation templates classes

D Programming Language

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

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

6.S096 Lecture 4 Style and Structure

Chapter 17 vector and Free Store

CS201 Latest Solved MCQs

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

CS558 Programming Languages

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

Transcription:

CS11 Advanced C++ Fall 2006-2007 Lecture 7

Today s Topics Explicit casting in C++ mutable keyword and const Template specialization Template subclassing

Explicit Casts in C and C++ C has one explicit cast operator for everything Can convert between related types e.g. double to int Can cast pointers to different types e.g. void* to float*, or float* to char* Can cast pointers to int, and vice versa Can cast away const-ness Many unsafe or potentially unsafe scenarios! C++ provides four explicit casting operators Breaks down different kinds of casts into explicit operations Provides more type-safety checks at compile-time, run-time Easier for programmers to understand, too!

C++ Cast Operations const_cast Casts away const-ness static_cast Performs safe conversions between related types, using static type information dynamic_cast Uses runtime type information to safely cast down or across class hierarchies reinterpret_cast For all the dangerous stuff.

Removing const Constraints const_cast removes const constraints T const_cast<t>(const T value) Not for changing the type of value! Using const_cast to change types will not compile Examples: void update(specialwidget *psw); const SpecialWidget csw; update(&csw); // COMPILE ERROR update(const_cast<specialwidget*>(&csw)); // OK Widget *pw = new SpecialWidget(); update(const_cast<specialwidget*>(pw)); // COMPILE ERROR

Cached Values and const const_cast sometimes used when objects cache temporary values that are expensive to compute class Date {... string cache; bool cachevalid; void updatecachevals(); // sets cache value public:... string stringrep() const; }; stringrep() returns a string version of the date value Cache the result, as long as date value doesn t change

Cached Values and const (2) Implementation of stringrep() string Date::stringRep() const { if (!cachevalid) { // this is const, so cast to non-const Date* Date *mut = const_cast<date*>(this); mut->updatecachevals(); mut->cachevalid = true; } return cache; } this is const because the member function is const Must cast away const to update cached values!

Cached Values and const (3) That solution isn t very elegant. Also, it might not actually work! If original variable is declared as const, casting away const is not guaranteed to work on all implementations. For example, system might apply access constraints to the memory used by the variable Example: Date d1; const Date d2; string s1 = d1.stringrep(); string s2 = d2.stringrep(); //??? s2 is not guaranteed to have a valid result across all implementations

Mutable Data-Members Better solution is to use the mutable keyword class Date {... mutable string cache; mutable bool cachevalid; void updatecachevals() const; public:... string stringrep() const; }; Compiler will ensure that cache can be changed, even on variables declared const mutable means can never be const

Mutable Data-Members (2) Implementation of stringrep() gets simpler: string Date::stringRep() const { if (!cachevalid) { // Only mutable data-members are changed. updatecachevals(); cachevalid = true; } return cache; } And this also works now: Date d1; const Date d2; string s1 = d1.stringrep(); string s2 = d2.stringrep(); // OK!

Proper Uses of const_cast const_cast is best for situations when a function doesn t use const, but it ought to! The function doesn t change the argument, but argument wasn t declared const (usually by mistake) Example: dealing with bad standard libraries size_t strlen(char *s); // should be const char * const char *prompt = "Shall we play a game?"; size_t length = strlen(const_cast<char*>(prompt)); Very uncommon situation! You shouldn t need const_cast very often

Static Casts static_cast converts between related types static_cast<t>(value) Converts value to type T Only uses static type information (compile-time check) Example use-cases: Convert from an integral type to an enumeration Convert from a floating-point type to an integral type Convert from one pointer-type to another Typically, within the same class hierarchy Example: int *p = static_cast<int*>(malloc(100 * sizeof(int))); malloc() returns a void* which needs to be casted

Dynamic Casts dynamic_cast converts between types in a class hierarchy, using run-time type information dynamic_cast<t>(value) T must be a pointer or reference to a polymorphic type (a class with virtual member-functions) Performs a run-time type-check to see if value can be cast to T If so, conversion takes place and result has type T If not, dynamic_cast evaluates to 0! Mainly used to cast a base-class pointer/reference to a derived class. Has very complex behavior with multiple inheritance!

Static and Dynamic Casts Static casts are faster than dynamic casts Compile-time check vs. a runtime check Static casts can t move down a class hierarchy Requires run-time information about objects Example: class Widget { }; class SpecialWidget : public Widget { }; Widget *pw = new SpecialWidget(); // COMPILE ERROR: static type of pw is Widget* SpecialWidget *spw = static_cast<specialwidget*>(pw); // OK: pw points to a SpecialWidget SpecialWidget *spw = dynamic_cast<specialwidget*>(pw);

Static and Dynamic Cast Examples Dialog *d = new Dialog(); Window *w = static_cast<window *>(d); // OK, compile-time check (fast) Button *b = new Button(); Component *c = dynamic_cast<component *>(b); // OK, runtime check (slower) Component Window Button Dialog Button *b2; b2 = static_cast<button *>(c); b2 = dynamic_cast<button *>(c); // COMPILE ERROR // OK, b2 == b Dialog *d2; d2 = static_cast<dialog *>(b); d2 = dynamic_cast<dialog *>(b); // COMPILE ERROR // OK, d2 == 0

Reinterpreting Values reinterpret_cast<t>(value) Converts between unrelated types Again, converts value to type T Example use-cases: Convert an integer to a pointer, or vice versa Convert a pointer to a completely unrelated pointer type If destination type is same bit-width, the result s bit-pattern is same as the source s bit-pattern Should need reinterpret_cast very rarely If you do need it, encapsulate this functionality inside a class, and don t expose it to others! (Make sure you really do need it!)

Reinterpreting Values (2) Example: An API that represents windows and other GUI components with handles /** Clears the specified window. **/ void clearwindow(int handle) { Window *pwnd = reinterpret_cast<window*>(handle);... } API users aren t exposed to implementation details A safer solution? The API could use an STL map (or hash_map) to associate integer handles with GUI components Slower, but less likely to fail spectacularly!

The Point Class-Template (a review) template<class T> class Point { T x_coord, y_coord; public: Point() : x_coord(0), y_coord(0) {} Point(T x, T y) : x_coord(x), y_coord(y) {} T getx() { return x_coord; } void setx(t x) { x_coord = x; }... }; Parameterized on coordinate-type, named T class T means any type, not just classes Can use typename instead of class means same thing

Other Template Paramters Can parameterize templates on constants template<class T, int dim> class SquareMatrix { protected: T values[dim * dim]; public: }; SquareMatrix() { setidentity(); } ~SquareMatrix() { }... Type and dimension are template-parameters Allocation is static, so no delete in destructor

A Vector Template Write a generic vector-template: template <class T> class Vector { T *vals; int size; public: Vector(); explicit Vector(int size); T& elem(int i) { return vals[i]; }... // etc. }; Can instantiate template for various types: Vector<int> vints; Vector<string> vstrs; Vector<Shape*> vshapeptrs; Vector<Node*> vnodeptrs;

Specialized Vectors! Could specialize Vector<bool> s storage Instead of one byte/word per bool, store each bool value in a bit (a bit-packed representation) Provide a specialization of Vector template template <> class Vector<bool> { int *packedvals; int size; public:... bool& elem(int i) {... } }; All Vector<bool> variables use this specialization Specialized version is a separate class-template Must implement all the members, etc.

More Vector Specialization Vector<T> typically used to store pointers When T is a class, avoids slicing and polymorphism issues Can lead to code-bloat too! C++ compilers typically replicate template-code for each instantiation Lots of different Vector<T> instantiations will mean larger binary-file size Solution: specialize Vector for pointer-types

Specializing Vector for Pointers (1) Implement a special version of Vector<T> template <> class Vector<void*> { // Array of void* values void **vals; int size; public:... void* & elem(int i) {... } }; Provides base implementation for all Vectors of pointers but is only used for Vector<void*> variables!

Specializing Vector for Pointers (2) Next, provide a partial specialization of Vector template <class T> class Vector<T*> : private Vector<void*> { // Vector<void*> provides data-members, etc. public: typedef Vector<void*> Base; Vector() { } explicit Vector(int size) : Base(size) { }... T* & elem(int i) { return reinterpret_cast<t* &>(Base::elem(i)); } }; User specifies T* (e.g. Vector<Shape*>) Compiler uses Vector<T*> with Shape for T

Specializing Vector for Pointers (3) Why subclass with private modifier? template <class T> class Vector<T*> : private Vector<void*> { Use Vector<void*> implementation in Vector<T*> Don t want to expose Vector<void*> interface! private allows a class to use parent-class functionality, without affecting its own interface Why reinterpret_cast? T* & elem(int i) { } return reinterpret_cast<t* &>(Base::elem(i)); The void* is reinterpreted as a completely different type

Analysis of Vector-Approach Vector<void*>, etc. is some nasty code void* is rarely a good idea: untyped pointers Same with reinterpret_cast ignoring types! There are definite, measurable benfits: Far less template code generated by this method Compiled programs are smaller, faster Compile times can also be dramatically reduced Needed infrequently, but a very effective technique!

Subclassing Templates You write a template for a base-class: template<typename T> class Base { public: void f() { } }; Then you write a template for a derived class: template<typename T> class Derived : public Base<T> { public: void g() { f(); } }; This code won t compile!

Subclassing Templates (2) Inside Derived<T>::g(), the name f doesn t depend on the template-parameter T template<typename T> class Derived : public Base<T> { public: void g() { f(); } }; Known as a nondependent name Compiler doesn t look in dependent base-classes when looking up nondependent names i.e. compiler doesn t check Base<T> when looking for f

Subclassing Templates (3) Use this->f() instead of just f() this is always implicitly dependent in a template e.g. this has a type of Derived<T>* in this example Use Base<T>::f() instead of just f() Don t do this if f is virtual! You might not get the right results.

More Template Subclassing Fun You write this: void f() { } // A global function f template<typename T> class Base { public: void f() { } }; template<typename T> class Derived : public Base<T> { public: void g() { f(); } }; This code does compile!

More Template Subclassing Fun (2) When compiler tries to resolve f, it searches enclosing scope of Derived<T> That scope contains the global function f Qualified name of f is ::f() The code compiles, and ::f() is called instead of Base<T>::f() Moral: Be very careful when subclassing templates! Often need this-> or Base<T>:: for base-class member access, when deriving from a template

This Week s Assignment Small project (emphasis on small ) Experiment with something interesting to you The Standard Template Library C++ multiple-inheritance Create some templates Write some interesting program or utility Progress check-up in a week