Default and Copy Constructors
Default and Copy Constructors
Steven J. Zeil
Old Dominion University, Dept. of Computer Science
Copyright © 1999-2008 Steven J. Zeil, Old Dominion University
Table of Contents
1. The Default Constructor
1.1. Declaring a Default Constructor
1.2. Why 'default'?
1.3. Implicit Use: Arrays
1.4. Implicit Use: Other Constructors
1.5. Explicit Construction in Other Constructors
1.6. Explicit Construction: Initializer Lists
1.7. Initializer Lists
1.8. The Helpful Compiler
1.9. Example: Name
1.10. Example: Name 2
1.11. Example: Name 3
1.12. Example: BidCollectionBook implicit default constructor
1.13. Example: BidCollectionBook explicit default constructor
2. Copy Constructors
2.1. Where Do We Use a Copy Constructor?
2.2. Compiler-Generated Copy Constructors
2.3. Example: Bid: Compiler-Generated Copy Constructor
2.4. Example: BidCollection: Compiler-Generated Copy Constructor
2.5. Example: BidCollection: harder to copy
2.6.
2.7.
2.8.
2.9.
2.10.
2.11. Trouble: bids is corrupted
2.12. That's not the worst of it!
2.13. What a Mess!
2.14. Avoiding this Problem
2.15. Writing a BidCollection Copy Constructor
2.16. Once More, With Feeling!
2.17.
2.18.
2.19.
2.20.
2.21. Much Nicer
2.22. Shallow & Deep Copying
3. Assignment
3.1. Compiler-Generated Assignment Ops
3.2. Example: BidCollection: Guess what happens
3.3.
3.4.
3.5.
3.6.
3.7. Again: bids is corrupted
3.8. Avoiding this Problem