Script started on Thu Apr 24 22:41:54 2003 [root@Fizzgig /root]# cd C [root@Fizzgig C]# cat makefile thing: thing.o main.o g++ -g thing.o main.o -o thing main.o: main.cpp thing.hpp g++ -c main.cpp thing.o: thing.cpp thing.hpp g++ -c thing.cpp [root@Fizzgig C]# cat thing.hpp class Thing{ private: char* name; int serial; public: Thing(char* name = ""); Thing(Thing const &rhs); ~Thing(); Thing operator=(Thing const rhs); void show(); char* getName(); }; char* duplicateString(char* string); extern int thingCounter; [root@Fizzgig C]# cat thing.cpp /* Implementation of a Thing class, */ /* which is used to demonstrate constructors, destructors, */ /* and copy constructors. */ /* Author: Zach Tomaszewski */ /* Date: 24 April 2003 */ #include "thing.hpp" #include //in order to print to screen #include //in order to copy a string static int thingCount = 0; //used to generate serial numbers //Constructors and destructors Thing::Thing(char* name = ""){ this->name = duplicateString(name); this->serial = thingCount++; printf("Constructing: %s (%d)\n", this->name, this->serial); } Thing::Thing(Thing const &rhs){ this->name = duplicateString(rhs.name); this->serial = thingCount++; printf("Copy constructing: %s (%d)\n", this->name, this->serial); } Thing::~Thing(){ printf("Deleting: %s (%d)\n", this->name, this->serial); delete this->name; } //Operators /* Assignment */ Thing Thing::operator=(Thing const rhs){ if (this == &rhs) { //trying to copy to self; do nothing }else { delete this->name; //free space first this->name = duplicateString(rhs.name); } printf("Assigning %s (%d) to (%d)\n", rhs.name, rhs.serial, this->serial); return *this; } //Methods /* Prints this Thing to the screen */ void Thing::show(){ printf("Show: %s (%d)\n", this->name, this->serial); } /* Returns a pointer to this Thing's name [Dangerous: not a pointer to a copy, but the name itself] */ char* Thing::getName(){ return this->name; } //Utility functions /* Returns a pointer to a copy of the given string */ char* duplicateString(char* string){ char* copy = new char[strlen(string)+1]; strcpy(copy, string); return copy; } [root@Fizzgig C]# make g++ -c thing.cpp g++ -c main.cpp g++ -g thing.o main.o -o thing [root@Fizzgig C]# thing Constructing: Arthur (0) Constructing: Beverly (1) Constructing: Charles (2) ***Starting main. Constructing: Eve (3) Constructing: Irene (4) Show: Arthur (0) Show: Charles (2) Show: Eve (3) Constructing: George (5) Constructing: Jane (6) ***First Checkpoint. Copy constructing: Charles (7) Show: Charles (2) Show: Charles (7) Show: Beverly (1) Show: George (5) Second CheckpointCopy constructing: Irene (8) Assigning Irene (8) to (1) Copy constructing: Irene (9) Deleting: Irene (8) Deleting: Irene (9) Show: Irene (1) Show: Irene (4) Copy constructing: Eve (10) Assigning Eve (10) to (6) Copy constructing: Eve (11) Deleting: Eve (10) Deleting: Eve (11) Show: Eve (6) Show: Eve (3) Copy constructing: Irene (12) Assigning Irene (12) to (4) Copy constructing: Irene (13) Deleting: Irene (12) Deleting: Irene (13) Show: Irene (4) ***Third Checkpoint. Copy constructing: Charles (14) Show: Charles (7) Show: Charles (14) ***Entering f Copy constructing: Eve (15) ***In f. Constructing: Tom (16) Show: Tom (16) Constructing: Katherine (17) Show: Katherine (17) ***Leaving f. Copy constructing: EVE (18) Deleting: Tom (16) Deleting: EVE (15) Assigning EVE (18) to (4) Copy constructing: EVE (19) Deleting: EVE (18) Deleting: EVE (19) ***Back out of f. Show: Eve (3) Show: EVE (4) ***Entering f again. Copy constructing: Charles (20) ***In f. Constructing: Tom (21) Show: Tom (21) Show: Katherine (17) ***Leaving f. Copy constructing: CHARLES (22) Deleting: Tom (21) Deleting: CHARLES (20) Show: CHARLES (22) Deleting: CHARLES (22) ***Back out of f again. Show: Charles (7) ***Entering f 3rd time. Constructing: Vivian (23) ***In f. Constructing: Tom (24) Show: Tom (24) Show: Katherine (17) ***Leaving f. Copy constructing: VIVIAN (25) Deleting: Tom (24) Deleting: VIVIAN (23) Assigning VIVIAN (25) to (7) Copy constructing: VIVIAN (26) Deleting: VIVIAN (25) Deleting: VIVIAN (26) ***Back out of f 3rd time. Show: VIVIAN (7) Deleting: George (5) Deleting: Eve (6) Deleting: Charles (14) ***Finished. Deleting: VIVIAN (7) Deleting: Eve (3) Deleting: Katherine (17) Deleting: EVE (4) Deleting: Charles (2) Deleting: Irene (1) Deleting: Arthur (0) [root@Fizzgig C]# exit Script done on Thu Apr 24 22:42:55 2003