home *** CD-ROM | disk | FTP | other *** search
- //: C11:Autocc.cpp
- // From Thinking in C++, 2nd Edition
- // Available at http://www.BruceEckel.com
- // (c) Bruce Eckel 1999
- // Copyright notice in Copyright.txt
- // Automatic copy-constructor
- #include <iostream>
- #include <cstring>
- using namespace std;
-
- class WithCC { // With copy-constructor
- public:
- // Explicit default constructor required:
- WithCC() {}
- WithCC(const WithCC&) {
- cout << "WithCC(WithCC&)" << endl;
- }
- };
-
- class WoCC { // Without copy-constructor
- static const int bsz = 30;
- char buf[bsz];
- public:
- WoCC(const char* msg = 0) {
- memset(buf, 0, bsz);
- if(msg) strncpy(buf, msg, bsz);
- }
- void print(const char* msg = 0) const {
- if(msg) cout << msg << ": ";
- cout << buf << endl;
- }
- };
-
- class Composite {
- WithCC withcc; // Embedded objects
- WoCC wocc;
- public:
- Composite() : wocc("Composite()") {}
- void print(const char* msg = 0) {
- wocc.print(msg);
- }
- };
-
- int main() {
- Composite c;
- c.print("contents of c");
- cout << "calling Composite copy-constructor"
- << endl;
- Composite c2 = c; // Calls copy-constructor
- c2.print("contents of c2");
- } ///:~
-