Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

Similar documents
Laboratorio di Tecnologie dell'informazione

OBJECT ORIENTED PROGRAMMING USING C++

W3101: Programming Languages C++ Ramana Isukapalli

Laboratorio di Tecnologie dell'informazione

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

CS11 Advanced C++ Fall Lecture 7

C++ Programming: Polymorphism

C++ Casts and Run-Time Type Identification

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

Where do we stand on inheritance?

Dr. Md. Humayun Kabir CSE Department, BUET

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

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

G52CPP C++ Programming Lecture 13

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

Programmazione. Prof. Marco Bertini

Operators. The Arrow Operator. The sizeof Operator

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

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

Laboratorio di Tecnologie dell'informazione

More Advanced Class Concepts

Instantiation of Template class

Inclusion Polymorphism

C++#Overview#(1)# Topics# COS320# Heejin#Ahn#

Chapter 5 Object-Oriented Programming

CS3157: Advanced Programming. Outline

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

ECE 3574: Dynamic Polymorphism using Inheritance

Constants, References

C++ Overview (1) COS320 Heejin Ahn

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

Introduction to C++ Systems Programming

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Pointers and Arrays. Introduction To Pointers. Using The "Address Of" The Operator & Operator. Using The Dereference. The Operator * Operator

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

CSE 431S Type Checking. Washington University Spring 2013

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

CS OBJECT ORIENTED PROGRAMMING

Variables. Data Types.

Lecture 20: templates

Introduction to C++ with content from

TPF Users Group Spring 2005

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

Using Enum Structs as Bitfields

Object Oriented Software Design II

Increases Program Structure which results in greater reliability. Polymorphism

G52CPP C++ Programming Lecture 20

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

CSE 333 Lecture C++ final details, networks

Side Effects (3A) Young Won Lim 1/13/18

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

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

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

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

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

Polymorphism Part 1 1

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Basic Types, Variables, Literals, Constants

C++ Inheritance II, Casting

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.

Software Design and Analysis for Engineers

Chapter 3 Function Overloading

Exercise: Inventing Language

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

G52CPP C++ Programming Lecture 15

Laboratorio di Tecnologie dell'informazione

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Contents. C++ As a Better C. Comments. User-defined Type Names

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

Advanced Systems Programming

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

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

COEN244: Class & function templates

Casting in C++ (intermediate level)

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

Introduction to Programming Using Java (98-388)

Function Declarations. Reference and Pointer Pitfalls. Overloaded Functions. Default Arguments

THE PROGRAMMING RESEARCH GROUP

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

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

Special Member Functions

Inheritance & Polymorphism. Object-Oriented Programming Spring 2015

COMP322 - Introduction to C++

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

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

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

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

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

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

Programming, numerics and optimization

CSI33 Data Structures

eingebetteter Systeme

Input/output. Remember std::ostream? std::istream std::ostream. std::ostream cin std::istream. namespace std { class ostream { /*...

G52CPP C++ Programming Lecture 16

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

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

Transcription:

Laboratorio di Tecnologie dell'informazione Ing. Marco Bertini bertini@dsi.unifi.it http://www.dsi.unifi.it/~bertini/

Const correctness

What is const correctness? It is a semantic constraint, enforced by the compiler, to avoid that a particular object marked as const should not be modified const can be used in various scopes: outside of classes at global/namespace scope: const double AspectRatio = 1.653; // much better than a C style define: #define ASPECT_RATIO 1.653

Class constants It s usable for static objects at file, function and block level It s usable also for class specific constants, e.g. for static and non-static data members: class VideoFrame { private: static const int PALFrameRate;... }; const int VideoFrame::PALFrameRate = 25;

Pointers and constancy We can specify that a pointer is constant, that the data pointed to is constant, that both are constant (or neither): char greeting[] = Hello ; char* p = greeting; // nothing is constant const char* p = greeting; //non-const pointer // const data char* const p = greeting; // const pointer // non-const data const char* const p = greeting; // everything is const

Pointers and constancy - cont. If const appears to the left of * then what is pointed to is constant, if it s on the right then the pointer is constant: const char* const p means that p is a constant pointer to constant chars according to this writing char const* p is the same of const char* p

References and constancy You can not change an alias, i.e. you can t reassign a reference to a different object, so: Fred& const x makes no sense (it s the same thing of Fred& x), however: const Fred& x is OK: you can t change the Fred object using the x reference.

Functions and constancy The most powerful use of const is its application to function declarations: we can refer to function return value, function parameters and (for member functions) to the function itself Helps in reducing errors, e.g. you are passing an object as parameter using a reference/pointer and do not want to have it modified: void foo(const bar& b); // b can t be modified // use const params whenever possible

const return value Using a const return value reduces errors in client code, e.g. class Rational { //...}; const Rational operator*(const Rational& lhs, const Rational& rhs); Rational a,b,c; // let s say we missed an = // to make a comparison... (a*b)=c; // it s now illegal thanks to // const return value!

const return value - cont. When returning a reference probably it s better to return it as constant or it may be used to modify the referenced object: class Person { public: string& badgetname() const; // returns a reference to _name //... private: string _name; }; void mycode(const Person& p) { p.badgetname() = Igor ; // can change the _name // attribute of Person }

const member functions The purpose of const member functions is to identify which functions can be invoked on const objects. These functions inspect and do not mutate an object. NOTE: it s possible to overload methods that change only in constancy! It s useful if you need a method to inspect and mutate with the same name

const member functions - cont. class TextBlock { public: const char& operator[](size_t pos) const { return text[pos]; } char& operator[](size_t pos) { // has to be reference return text[pos]; // to be modifiable: } // C++ returns by value! private: string text; }; this is useful when dealing with objects that are passed as const references: void print(const TextBlock& ctb, size_t pos) { cout << ctb[pos]; // calls the const version of [] };

const member functions - cont. C++ compilers implemement bitwise constancy, but we are interested in logical constancy, e.g. the const reference return value seen before or we may need to modify some data member within a const method (declared mutable): class TextBlock { public: size_t length() const; private: string text; mutable size_t _length; mutable bool isvalidlenght; }; size_t TextBlock::length() const { if(!isvalidlengt) { _length=text.size(); isvalidlength=true; } return _length; }

const member functions - cont. To avoid code duplication between const and non-const member functions that have the same behaviour can be solved: putting common tasks in private methods called by the two versions of the const/nonconst methods casting away constancy, with the non-const method calling the const method (see later)

C++ and casting

C++ casting In the previous example we have seen how to cast an object to a const version of itself and then how to cast away constancy C++ casts are more restriced than C style casts In general the lesser we cast the better: C++ is a type safe language and casts subvert this behaviour we used those casts to eliminate code duplication: the benefits are worth the risk

C and C++ casts C style casts, to cast an expression to be of type T: (T) expression T(expression) C++ style casts: const_cast<t>(expression) dynamic_cast<t>(expression) static_cast<t>(expression) reinterpret_cast<t>(expresison)

const_cast const_cast is used to cast away the constness of an object It s the only cast that can do it

static_cast static_cast forces implicit conversions, such as non-const objects to const objects (as seen in const/non-const methods), int to double, void* to typed pointers, pointer-to-base to pointer-to-derived (but no runtime check). it s the most useful C++ style cast int j = 41; int v = 4; float m = j/v; // m = 10 float d = static_cast<float>(j)/v; // d = 10.25 BaseClass* a = new DerivedClass(); static_cast<derivedclass*>(a)->derivedclassmethod();

static_cast - cont. Prefer static_cast over C style cast, because we get the type safe conversion of C++: class MyClass : public MyBase {...}; class MyOtherStuff {...} ; MyBase *psomething; // filled somewhere MyClass *pmyobject; pmyobject = static_cast<myclass*>(psomething); // Safe; as long as we checked pmyobject = (MyClass*)(pSomething); // Same as static_cast<> // Safe; as long as we checked but harder to read MyOtherStuff *pother; pother = static_cast<myotherstuff*>(psomething); // Compiler error: Can't convert pother = (MyOtherStuff*)(pSomething); // No compiler error. // Same as reiterpret_cast<> and it's wrong!!!

const member functions Let s review again how to avoid code duplication between const and non-const member functions... the non-const method calls the const method and then cast away its constancy with const_cast

const member functions - cont. class TextBlock { public: const char& operator[](size_t pos) const { //... checks over boundaries, etc. //... return text[pos]; } char& operator[](size_t pos) { return const_cast<char&>( // take away constancy static_cast<const TextBlock&>(*this)[pos] // add constancy ); } //... };

const member functions - cont. class TextBlock { public: const char& operator[](size_t pos) const { //... checks over boundaries, etc. //... return text[pos]; } char& operator[](size_t pos) { return const_cast<char&>( // take away constancy static_cast<const TextBlock&>(*this)[pos] // add constancy ); } //... }; First cast to const, to call the const method, then remove const-ness

dynamic_cast dynamic_cast performs safe (runtime check) downcasting: i.e. determines if an object is of a particular type in an inheritance hierarchy. it has a runtime cost depending on the compiler implementation class Window { //... }; class SpecialWindow : public Window { public: void blink(); }; Window* pw; //...pw may point to whatever object // in Window hierarchy if( SpecialWindow* psw=dynamic_cast<specialwindow*>pw ) psw->blink();

reinterpret_cast reinterpret_cast is used for low-level casts, e.g. to perform conversions between unrelated types, like conversion between unrelated pointers and references or conversion between an integer and a pointer. It produces a value of a new type that has the same bit pattern as its argument. It is useful to cast pointers of a particular type into a void* and subsequently back to the original type. may be perilous: we are asking the compiler to trust us...

Credits These slides are (heavily) based on the material of: Marshall Cline, C++ FAQ Lite Scott Meyers, Effective C++, 3rd edition, Addison-Wesley