cs212: object oriented analysis and design lecture 10: copy constructor-ii
TRANSCRIPT
![Page 1: CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor-II](https://reader036.vdocuments.mx/reader036/viewer/2022082713/56649e905503460f94b94eb0/html5/thumbnails/1.jpg)
CS212: Object Oriented Analysis and DesignLecture 10: Copy constructor-II
![Page 2: CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor-II](https://reader036.vdocuments.mx/reader036/viewer/2022082713/56649e905503460f94b94eb0/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022082713/56649e905503460f94b94eb0/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022082713/56649e905503460f94b94eb0/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022082713/56649e905503460f94b94eb0/html5/thumbnails/5.jpg)
Deep vs Shallow copy
• Copy constructor is called only for initializations
• For assignment ? Operator overload ??