CS107L Handout 02 Autumn 2007 October 5, 2007 Copy Constructor and operator=



Similar documents
CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

C++ INTERVIEW QUESTIONS

Object Oriented Software Design II

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

C++FA 5.1 PRACTICE MID-TERM EXAM

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

CS107L Handout 04 Autumn 2007 October 19, 2007 Custom STL-Like Containers and Iterators

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

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

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

C++ Overloading, Constructors, Assignment operator

Semantic Analysis: Types and Type Checking

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Advanced Data Structures

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

13 Classes & Objects with Constructors/Destructors

Compile-time type versus run-time type. Consider the parameter to this function:

1. Polymorphism in C++...2

Object Oriented Software Design II

C++ Crash Kurs. C++ Object-Oriented Programming

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

An Incomplete C++ Primer. University of Wyoming MA 5310

Object Oriented Software Design II

CISC 181 Project 3 Designing Classes for Bank Accounts

A Grammar for the C- Programming Language (Version S16) March 12, 2016

Basics of C++ and object orientation in OpenFOAM

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Ch 7-1. Object-Oriented Programming and Classes

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

Curriculum Map. Discipline: Computer Science Course: C++

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Lecture 9. Semantic Analysis Scoping and Symbol Table

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Binary storage of graphs and related data

Passing 1D arrays to functions.

Chapter 2: Elements of Java

Moving from CS 61A Scheme to CS 61B Java

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

N3458: Simple Database Integration in C++11

Common Beginner C++ Programming Mistakes

Comp151. Definitions & Declarations

Variable Base Interface

C++FA 3.1 OPTIMIZING C++

Sources: On the Web: Slides will be available on:

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Class 16: Function Parameters and Polymorphism

Copy Constructors and Assignment Operators

El Dorado Union High School District Educational Services

Certified PHP Developer VS-1054

Chapter One Introduction to Programming

The C Programming Language course syllabus associate level

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

J a v a Quiz (Unit 3, Test 0 Practice)

CS 111 Classes I 1. Software Organization View to this point:

Computer Programming C++ Classes and Objects 15 th Lecture

Java Interview Questions and Answers

Conversion Constructors

Constructor, Destructor, Accessibility and Virtual Functions

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

C++ Programming Language

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Applied Informatics C++ Coding Style Guide

Glossary of Object Oriented Terms

Compiler I: Syntax Analysis Human Thought

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Compiler Construction

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Basic Object-Oriented Programming in Java

More C++ Concepts. Operator overloading Friend Function This Operator Inline Function

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

5 CLASSES CHAPTER. 5.1 Object-Oriented and Procedural Programming. 5.2 Classes and Objects 5.3 Sample Application: A Clock Class

CMSC 202H. ArrayList, Multidimensional Arrays

Lab Experience 17. Programming Language Translation

Programming Languages CIS 443

Object Oriented Software Design

Lecture 5: Java Fundamentals III

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

To Java SE 8, and Beyond (Plan B)

Java Basics: Data Types, Variables, and Loops

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

CS 101 Computer Programming and Utilization

Common Data Structures

Note: Syntactically, a ; is needed at the end of a struct definition.

Answers to Review Questions Chapter 7

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

Programming languages C

Transcription:

CS107L Handout 02 Autumn 2007 October 5, 2007 Copy Constructor and operator= Much of the surrounding prose written by Andy Maag, CS193D instructor from years ago. The compiler will supply a default (zero-argument) constructor if the programmer doesn t. This default constructor will call each data member s default constructor in order to fully initialize the object. If the programmer wishes to displace the default constructor, he simply provides one. The same thing goes for the destructor. There is another type of compiler-synthesized constructor the copy constructor, and it's called whenever an object needs to be constructed from an existing one. Suppose we had a class to encapsulate strings, and we call this class string. The details of the dynamic memory management are annoying enough that we might want to abstract those details away. // string.h, take one class string public: string(const char *str = ""); ~string(); ; private: initializefrom(const char *front, const char *end) char *front; char *end; // string.cc take one string::string(const char *str) initializefrom(str, str + strlen(str)); string::~string() delete[] front; void string::initializefrom(const char *front, const char *end) int length = end - front; this->front = new char[length + 1]; this->end = this->front + length; strncpy(this->front, front, length + 1); // include the '\0'

For this example, we ll assume that the string constructor allocates space for the characters, and the destructor frees up that same space. Internally, the are two iteratorlike points that point to the leading character and the null character at the end. The The copy constructor may be called when doing simple initializations of a string object: string professor("professor Plum"); string clone = professor; // copy constructor gets called. More importantly, the copy constructor is called when passing an object by value, or returning an object by value. For instance, we might have a function that translates a sentence to Pig Latin: static string translate(string sentence) // tokenizes, rotates, and rebuilds translation We might declare a string and call the translate function like this: string excuse("i need an extension because I forgot I was in CS107L."); cout << "Translation: \"" << translate(excuse) << "\"" << endl; 2 When passing the sentence object, the copy constructor is called to copy the string object from the calling function to the local parameter in the translate function. Because we did not specify a copy constructor, the default copy constructor is called. In this case, it s just not what we want. Default Copy Constructor The default copy constructor does a member-wise copy of all the primitive and embedded object fields. For our string class, the default copy constructor simply copies the two pointers that are embedded inside, as it would for any set of pure primitive types. However, the characters themselves are not copied. This is called a shallow copy, because it only copies the data down to one level. In memory, what we d have would look like this: excuse I n e e d L. \0 sentence

This causes a problem because now the two objects share the same characters data. If we changed any of the characters within the sentence object, it would change the characters in the excuse as well. What s worse is that after the translate function exits, the string destructor is called on the sentence object, which frees the character array more officially owned by excuse. Bad times. What We Want: The Deep Copy In order to avoid potential disasters like this, what we want is a deep copy of the string object. That means we want the copy constructor to synthesize of new character array that s a logical clone of the original, but memory independent of it. What we want is a picture in memory which looks like this: 3 excuse I n e e d L. \0 sentence I n e e d L. \0 Now we can manipulate the characters within the sentence object and not affect the original excuse object. When the character array within the copy is freed on exit the translate function, the original string object is still going strong. The copy constructor (be it the bad compiler-provided one or the correct user-defined one) is also invoked when returning an object by value. We didn t even begin to hit on those problems, though they are effectively the same. Declaring a Copy Constructor In order to provide deep-copy semantics for our string object, we need to declare our own copy constructor. A copy constructor takes a constant reference to an object of the class type as a parameter and has no return type. We would declare the copy constructor for the string within the class declaration like this: // string.h take two class string public: string(const char *str = ""); string(const string& original); ~string(); ; private: void initializefrom(const char *front, const char *end); char *front; char *end;

4 We d implement the copy constructor like this: string::string(const string& original) initializefrom(original.front, original.end); You should provide a copy constructor for any class for which you want deep copy semantics and the compiler will do the wrong thing. If the class only has data members that are integral types (that is, it contains no pointers or open files) and/or direct objects (which have properly implemented copy constructors), you can omit the copy constructor and let the default copy constructor handle it. Limitations The copy constructor is not called when doing an object-to-object assignment. For instance, if we had the following code, we would still only get a shallow copy: string betty("betty Rubble"); string wilma; wilma = betty; // Initializes string to "Betty Rubble" // Initializes string to empty string This is because the assignment operator is being called instead of the copy constructor. By default, the assignment operator does a member-wise copy of the object, which in this case gives a shallow copy. However, C++ gives us the ability to override the default assignment operator, and we ll learn that today too. Incidentally, there may be cases when you want to prevent anyone from copying a class object via the copy constructor. By declaring a copy constructor as a private constructor within the class definition and not implementing it, the compiler will prevent the passing of objects of that class by value. Assignment It is possible to redefine the assignment operator for a class. The assignment operator must be defined as a member function in order to guarantee that the left-hand operand is an object. By default, the assignment operator is defined for every class, and it does a member-wise copy from one object to another. This is a shallow copy, so the assignment operator should generally be redefined for any class that contains pointers. You can declare the assignment operator as a private operator and not provide an implementation in order to prevent object-to-object assignment. The key difference between assignment and copy construction is that assignment possibly involves the destruction of embedded resources. We need to overload the assignment operator whenever we want to guarantee that memory (or perhaps other resources) are properly disposed of before they re replaced. The syntax for the assignment method is a trifle quirky, but it's just that: syntax, and you just treat the

prototype as boilerplate notation and otherwise implement the method as you would any other. // string.h take three class string public: string(const char *str = ""); string(const string& original); const string& operator=(const string& rhs); ~string(); 5 ; private: void initializefrom(const char *front, const char *end); char *front; char *end; // addition to string.cc const string& string::operator=(const string& rhs) if (this!= &rhs) delete[] front; initializefrom(rhs.front, rhs.end); return *this; A good way to look at the assignment operator: Think of it as destruction followed by immediate reconstruction. The delete[] would appear in the destructor as well, whereas the next three lines are very constructor-ish. Caveats 1: Caveat 2: The this!= &rhs checks to see whether we're dealing with a selfassignment. An expression of the form me = me is totally legal, but there's no benefit in actually destroying the object only to reconstruct it only to take the same form. In fact, the delete[] line would actually release the very memory that we need on the very next line. The return type is const string&, and the return statement is return *this, because cascaded assignments involving strings should behave and propagate right to left just as they would with ints or doubles. string heroine("ginger"); string cluck("babs"); string quack("rhodes"); string meanie("mrs. tweety"); meanie = quack = cluck = heroine; cout << heroine << cluck << quack << meanie << endl; // should print: gingergingergingerginger

6 Problem Consider the following C++ struct definitions: struct scooby scooby(const string& jones) fred = jones; daphne = new string(fred); scooby(const scooby& blake) : daphne(null), fred(blake.fred) ~scooby() delete daphne; string fred; string *daphne; ; struct dooby dooby(const string& shaggy, const string velma) : doo(velma), scrappy(shaggy) const string scrappy; scooby doo; ; Assume that the whereareyou function has just been called. Specify the ordering for all the calls to the string memory management routines: the zero-argument constructor, the (char*) constructor, the copy constructor, the operator= assignment operator, and the destructor. Answer: static void whereareyou(const string& astro, const string& dino) dooby lassie(astro, dino); dooby tiger(lassie); copy constructor initializing lassie.velma out of dino copy constructor initializing lassie.scrappy out of shaggy zero arg constructor initializing lassie.doo.fred operator= reassigning lassie.doo.fred to be a clone of jones copy constructor initializing a dynamically allocated string (address assigned to lassie.doo.daphne) destructor to dispose of local paramter velma copy constructor initializing tiger.scrappy to be a clone of lassie.scrappy copy constructor for tiger.doo.fred destructor disposing of tiger.doo.fred destructor disposing of tiger.scrappy destructor disposing of heap-based string pointed to by lassie.doo.daphne destructor disposing of lassie.doo.fred destructor disposing of lassie.scrappy