Advanced C++ 4/13/2017. The user. Types of users. Const correctness. Const declaration. This pointer and const.

Similar documents
Chapter 16 Templates and Inheritance

Pointers. Developed By Ms. K.M.Sanghavi

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

COMP6771 Advanced C++ Programming

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Ch. 11: References & the Copy-Constructor. - continued -

Part X. Advanced C ++

IS 0020 Program Design and Software Tools

Intermediate Programming, Spring 2017*

More on Templates. Shahram Rahatlou. Corso di Programmazione++

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Lecture 2 Polymorphism, Traits, Policies, and Inheritance

Fast Introduction to Object Oriented Programming and C++

Midterm Review. PIC 10B Spring 2018

Homework 4. Any questions?

Short Notes of CS201

COMP6771 Advanced C++ Programming

Ch. 12: Operator Overloading

CSCE 110 PROGRAMMING FUNDAMENTALS

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

CS201 - Introduction to Programming Glossary By

Smart Pointers. Some slides from Internet

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

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

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Part III. Advanced C ++

05-01 Discussion Notes

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

CS93SI Handout 04 Spring 2006 Apr Review Answers

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

Introduction to Linked Lists. Introduction to Recursion Search Algorithms CS 311 Data Structures and Algorithms

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

EL2310 Scientific Programming

G52CPP C++ Programming Lecture 20

Object-Oriented Programming for Scientific Computing

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

Templates. Zoltán Porkoláb: C++11/14 1

04-24/26 Discussion Notes

AN OVERVIEW OF C++ 1

Inheritance, and Polymorphism.

Introduction to Programming

Chapter 13: Copy Control. Overview. Overview. Overview

CS

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Polymorphism Part 1 1

Intermediate Programming, Spring 2017*

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Linked List using a Sentinel

LECTURE 03 LINKED LIST

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

G52CPP C++ Programming Lecture 16

6 Architecture of C++ programs

Tokens, Expressions and Control Structures

Programming in C++: Assignment Week 8

CPSC 427: Object-Oriented Programming

Term Paper. Daniel Sarnow. Interface-based Programming in C++ Fakultät Technik und Informatik Studiendepartment Informatik

CS 376b Computer Vision

Introduction to C++ Systems Programming

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Object-Oriented Programming for Scientific Computing

Instantiation of Template class

Program template-smart-pointers-again.cc

Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370

Variables. Data Types.

CSI33 Data Structures

Copy Control 2008/04/08. Programming Research Laboratory Seoul National University

Final Exam. Name: Student ID: Section: Signature:

CS250 Final Review Questions

Functions and Recursion

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

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

Advanced Systems Programming

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Program template-smart-pointers.cc

C++_ MARKS 40 MIN

Computer Science II CSci 1200 Lecture 24 C++ Inheritance and Polymorphism

Absolute C++ Walter Savitch

CS 162, Lecture 25: Exam II Review. 30 May 2018

Pointers and References

Lecture 10. To use try, throw, and catch Constructors and destructors Standard exception hierarchy new failures

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts


EL2310 Scientific Programming

CS201 Latest Solved MCQs

An introduction to. Templates. Generic Programming. Good old C. Metaprogramming 4/13/2017

Exceptions, Case Study-Exception handling in C++.

CSE 333 Lecture smart pointers

COMP6771 Advanced C++ Programming

A Tour of the C++ Programming Language

C++ Exception Handling 1

C++ Programming Lecture 7 Software Engineering Group

Member functions 21/11/2018. Accessing member variables. Accessing member variables

Resource Management With a Unique Pointer. Resource Management. Implementing a Unique Pointer Class. Copying and Moving Unique Pointers

Modernizing legacy C++ code

Introduction Of Classes ( OOPS )

! Errors can be dealt with at place error occurs

Transcription:

The user. Advanced C++ For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 #define private public #define protected public #define class struct Source: Lutz Kettner. Piyush Kumar Types of users Const correctness Revision. We can distinguish between two kinds of protection a design can provide: a user that makes occasional mistakes a user that willingly tries to get around the protection mechanism int* p; int* const q; const int* r; const int* const s; // the pointer, the data it refers to // --------------------------------------- // the pointer, the data it refers to // --------------------------------------- int* p; // non-const non-const int* const q; // const non-const const int* r; // non-const const const int* const s; // const const Const declaration This pointer and const. struct A const int i; A() : i(42) struct C // hidden parameter: T* const this; void foo(); // hidden parameter: const T* const this; void bar() const; 1

Make Temporary Return Objects in C++ Classes Const Make Temporary Return Objects in C++ Classes Const L-values: can be used for the left side of an assignment, they are non-const. R-values: cannot be used for the left side of an assignment.. They are const. For example the post-increment operator requires an l-value, but is itself an r-value. Thus, we cannot write: int i; i++ ++; // second ++ forbidden! Or i++++; Error: error.cpp:5: error: non-lvalue in increment But: struct A A operator++ (int); // the post increment operator Now, lets try: A a; a++++; //compiles! Make Temporary Return Objects in C++ Classes Const Empty Classes C++ classes are often empty! It works, because a++ returns a temporary object of type A. But it probably does not do what one would expect. Since the second ++ works on a temporary object, a itself gets only incremented once. We can forbid the second increment explicitly by making the return type, the type of the temporary object, const. This should be considered for all similar temporary return types. struct A // the post increment operator const A operator++ (int); #include <iostream> using namespace std; struct X int main() cout << sizeof(x) << endl; return 0; Empty Classes Polymorphism #include <iostream> using namespace std; struct X class Y:public X int main() cout << sizeof(x) << endl; cout << sizeof(y) << endl; return 0; Recap: Ability to associate different specific behaviors with a single generic notation. (Many forms or shapes) What you have seen: Dynamic Polymorphism EBCO: Empty base class Optimization 2

Dynamic Polymorphism Example Static Polymorphism #include "coord.hpp" // concrete geometric object class Circle // - \bfseries not derived from any class class Circle void draw() const; Coord center_of_gravity() const; //... // concrete geometric object class Line // - \bfseries not derived from any class class Line void draw() const; Coord center_of_gravity() const; //... Static Polymorphism #include "statichier.hpp" #include <vector> // draw any GeoObj template <typename GeoObj> void mydraw (GeoObj const& obj) obj.draw(); // call draw() according to type of object // process distance of center of gravity between two GeoObjs template <typename GeoObj1, typename GeoObj2> Coord distance (GeoObj1 const& x1, GeoObj2 const& x2) Coord c = x1.center_of_gravity() - x2.center_of_gravity(); return c.abs(); // return coordinates as absolute values // draw homogeneous collection of GeoObjs template <typename GeoObj> void drawelems (std::vector<geoobj> const& elems) for (unsigned i=0; i<elems.size(); ++i) elems[i].draw(); // call draw() according to type of element Static Polymorphism int main() Line l; Circle c, c1, c2; mydraw(l); mydraw(c); // mydraw<line>(geoobj&) => Line::draw() // mydraw<circle>(geoobj&) => Circle::draw() distance(c1,c2); // distance<circle,circle>(geoobj1&,geoobj2&) distance(l,c); // distance<line,circle>(geoobj1&,geoobj2&) // std::vector<geoobj*> coll; // ERROR: no heterogeneous // collection possible std::vector<line> coll; // OK: homogeneous collection possible coll.push_back(l); // insert line drawelems(coll); // draw all lines Static Polymorphism All types must be determined at compile time. Heterogeneous collections can no longer be handled transparently. Generated code is potentially faster than dynamic polymorphism. CRTP: Curiously recurring template pattern General class of techniques that consists of passing a derived class as a template argument to one of its own base classes. // The Curiously Recurring Template Pattern (CRTP) class derived : public base<derived> 3

CRTP Who is my parent? CRTP: Alternative outline. // The Curiously Recurring Template Pattern // (CRTP) template <typename Derived> class CuriousBase // // The Curiously Recurring Template Pattern // (CRTP) template <typename Derived> class CuriousBase // class Curious : public CuriousBase<Curious> // Only valid if the size of CuriousBase<Curious> // can be determined independently of Curious. template <typename T> class CuriousT : public CuriousBase<CuriousT<T> > CRTP: Alternative outline. // The Curiously Recurring Template Pattern // (CRTP) template < template<typename> class Derived > class MCuriousBase // template <typename T> class MoreCuriousT : public MCuriousBase<MoreCuriousT> A Generic solution to object counting. CRTP Concrete Example Counting Objects #include <stddef.h> template <typename CountedType> class ObjectCounter private: static size_t count; // number of existing objects protected: // default constructor ObjectCounter() ++count; // copy constructor ObjectCounter (ObjectCounter<CountedType> const&) ++count; // destructor ~ObjectCounter() --count; // return number of existing objects: static size_t live() return count; // initialize counter with zero template <typename CountedType> size_t ObjectCounter<CountedType>::count = 0; CRTP Concrete Example Counting Objects #include "objectcounter.hpp" #include <iostream> template <typename CharT> class MyString : public ObjectCounter<MyString<CharT> > //... int main() MyString<char> s1, s2; MyString<wchar_t> ws; CRTP and the current assignment the graph knows the node and the edge class that are supposed to work together, and therefore the graph class passes itself as template argument to both types. std::cout << "number of MyString<char>: " << MyString<char>::live() << std::endl; std::cout << "number of MyString<wchar_t>: " << ws.live() << std::endl; 4

Another CRTP application Implement Inequality in terms of equality. class A bool operator == (const A& a) const; bool operator!= (const A& a) const return! (*this == a); Another CRTP application Implement Inequality in terms of equality. template <class T> class Inequality bool operator!= (const T& t) const return! (static_cast<const T&>(*this) == t); class A : public Inequality<A> bool operator == (const A& a) const; More CRTP usage. The same technique can be used to implement a base class for iterators that contains all those small member functions that are defined in terms of a much smaller set of member functions. Proxy Classes Is this legal? Proxy classes int data[10][20]; void processinput(int dim1, int dim2) int data[dim1][dim2]; int *data = new int[dim1][dim2]; A dynamic two-dimensional array of integers could be declared in C++ as follows: class Array2D Array2D( int dim1, int dim2); 5

Proxy Classes Proxy Classes Of course, in a program we would like use the array similar to the builtin (static) two-dimensional arrays and access an element as follows: int main() Array2D a(5,10); int i = a[2][8]; // (a[2])[8] However, there is no operator[][] in C++. Instead, we can implement operator[] to return conceptually a onedimensional array, where we can apply operator[] again to retrieve the element. Proxy Classes Proxy classes class Array1D Array1D( int dim); int operator[](int i); class Array2D Array2D( int dim1, int dim2); Array1D& operator[](int i); The intermediate class Array1D is called proxy class, also known as surrogate [Item 30, Meyers97]. Conceptually, it represents a onedimensional array. In this application we surely do not want to copy the elements to actually create a onedimensional array. The proxy class will just behave as if it is an one-dimensional array and internally it will use a pointer to the two-dimensional array to implement its operations. Double Dispatch is a mechanism that dispatches a function call to different concrete functions depending on the runtime types of multiple objects involved in the call. Lookup (Myers Item 31, More effective C++) Smart pointers I shot an arrow into the air, It fell to earth, I know not where. The Arrow and the Song H. W. Longfellow 6

Smart Pointers: unique_ptr Smart Pointers: unique_ptr Typical pointer usage. void f() MyClass *ptrmyclass = new MyClass; // perform some operators delete ptrmyclass; A return in the middle of the function. An exception thrown. Or else the function has to catch all exceptions. How do we avoid resource leaks? Recap: valgrind? Source of trouble! What if you forgot a return in the middle? Smart Pointers: auto_ptr Smart Pointers: auto_ptr // Fixing the last program : Complicated. #include <memory> // header for unique_ptr void f() MyClass *ptr = new MyClass; try void f() // create and initialize a unique_ptr std::unique_ptr<myclass> ptr(new MyClass); // perform some operators catch( ) delete ptr; throw; // rethrow the exception delete ptr; delete and catch are no longer necessary! The smart pointer can free the data to which it points whenever the pointer itself gets destroyed. A unique_ptr is a pointer that serves as an owner of the object to which it refers to. As a result, the object gets destroyed when its unique_ptr gets destroyed. A requirement of unique_ptr is that its object has only one owner. unique_ptr Misusing unique_ptrs. Has much of the same interface as an ordinary pointer ( operator *, operator -> ) Pointer arithmetic (such as ++) is not defined. Note: Cannot share ownerships. Do not do reference counting. Do NOT meet the requirements for container elements. When a unique_ptr is copied/assigned the source unique_ptr gets modified! Because it transfers its value rather than copying it. std::auto_ptr<myclass> ptr1(new MyClass); // OK std::auto_ptr<myclass> ptr1 = new MyClass; // Error 7

Unit Testing unit testing is a procedure used to validate that individual units of source code are working properly. Unit = Smallest testable part of an application In C++, Smallest unit = Class Goal: Isolate each part of the program and show individual parts are correct. 8