cs212: object oriented analysis and design lecture 10: copy constructor-ii

5
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor-II

Upload: polly-hamilton

Post on 16-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor-II

CS212: Object Oriented Analysis and DesignLecture 10: Copy constructor-II

Page 2: CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor-II

Copy copy: revisited

Main

Constructor

Copy Constructor

h

f(): global function

x=h

1

2

323

432

Page 3: CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor-II

What happens if we don’t use it?

• Compiler creates one for you in the case of simple structures

• The default primitive behaviour: a bit-copy.

• C++ compiler will still automatically create a copy-constructor if you don’t make one

• The process the compiler goes through to synthesize a copy-constructor is called member-wise initialization.

Page 4: CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor-II

Issues with pointers

5 0x8975

5 0x8975

A

B

{B=A;

}

Initial object

Duplicate object

Page 5: CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor-II

Deep vs Shallow copy

• Copy constructor is called only for initializations

• For assignment ? Operator overload ??