Assalamu alaikum, can you compile these programs and tell me what you get?
Ayoub
//: C13:PStashTest.cpp // From Thinking in C++, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 2000 // Copyright notice in Copyright.txt //{L} PStash // Test of pointer Stash #include "PStash.h" #include "../TICPP-2nd-ed-Vol-one-code/require.h" #include <iostream> #include <fstream> #include <string> using namespace std; class Counted { int id; static int count; public: Counted() : id(count++){ cout << "Constructed id = " << id << endl;} ~Counted(){ cout << "Destructed id = " << id << endl;} friend ostream &operator<<(ostream &os, Counted &a); }; ostream &operator<<(ostream &os, Counted &a) { os << a.id; return os;} int main() { PStash intStash; // 'new' works with built-in types, too. Note // the "pseudo-constructor" syntax: for(int i = 0; i < 25; i++) intStash.add(new Counted); for(int j = 0; j < intStash.count(); j++) cout << "intStash[" << j << "] = " << *(Counted*)intStash[j] << endl; // Clean up: for(int k = 0; k < intStash.count(); k++) delete (Counted*)intStash.remove(k); ifstream in ("ch13_3.cpp"); assure(in, "ch13_3.cpp"); PStash stringStash; string line; while(getline(in, line)) stringStash.add(new string(line)); // Print out the strings: for(int u = 0; stringStash[u]; u++) cout << "stringStash[" << u << "] = " << *(string*)stringStash[u] << endl; // Clean up: for(int v = 0; v < stringStash.count(); v++) delete (string*)stringStash.remove(v); } ///:~
//: C13:PStash.cpp {O} // From Thinking in C++, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 2000 // Copyright notice in Copyright.txt // Pointer Stash definitions #include "PStash.h" #include "../TICPP-2nd-ed-Vol-one-code/require.h" #include <iostream> #include <cstring> // 'mem' functions using namespace std; int PStash::add(void* element) { const int inflateSize = 10; if(next >= quantity) inflate(inflateSize); storage[next++] = element; return(next - 1); // Index number } // No ownership: PStash::~PStash() { for(int i = 0; i < next; i++) require(storage[i] == 0, "PStash not cleaned up"); delete []storage; } // Operator overloading replacement for fetch void* PStash::operator[](int index) const { require(index >= 0, "PStash::operator[] index negative"); if(index >= next) return 0; // To indicate the end // Produce pointer to desired element: return storage[index]; } void* PStash::remove(int index) { void* v = operator[](index); // "Remove" the pointer: if(v != 0) storage[index] = 0; return v; } void PStash::inflate(int increase) { const int psz = sizeof(void*); void** st = new void*[quantity + increase]; memset(st, 0, (quantity + increase) * psz); memcpy(st, storage, quantity * psz); quantity += increase; delete []storage; // Old storage storage = st; // Point to new memory } ///:~
//: C13:PStash.h // From Thinking in C++, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 2000 // Copyright notice in Copyright.txt // Holds pointers instead of objects #ifndef PSTASH_H #define PSTASH_H class PStash { int quantity; // Number of storage spaces int next; // Next empty space // Pointer storage: void** storage; void inflate(int increase); public: PStash() : quantity(0), storage(0), next(0) {} ~PStash(); int add(void* element); void* operator[](int index) const; // Fetch // Remove the reference from this PStash: void* remove(int index); // Number of elements in Stash: int count() const { return next; } }; #endif // PSTASH_H ///:~
//: :require.h // From Thinking in C++, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 2000 // Copyright notice in Copyright.txt // Test for error conditions in programs // Local "using namespace std" for old compilers #ifndef REQUIRE_H #define REQUIRE_H #include <cstdio> #include <cstdlib> #include <fstream> #include <string> inline void require(bool requirement, const std::string& msg = "Requirement failed"){ using namespace std; if (!requirement) { fputs(msg.c_str(), stderr); fputs("\n", stderr); exit(1); } } inline void requireArgs(int argc, int args, const std::string& msg = "Must use %d arguments") { using namespace std; if (argc != args + 1) { fprintf(stderr, msg.c_str(), args); fputs("\n", stderr); exit(1); } } inline void requireMinArgs(int argc, int minArgs, const std::string& msg = "Must use at least %d arguments") { using namespace std; if(argc < minArgs + 1) { fprintf(stderr, msg.c_str(), minArgs); fputs("\n", stderr); exit(1); } } inline void assure(std::ifstream& in, const std::string& filename = "") { using namespace std; if(!in) { fprintf(stderr, "Could not open file %s\n", filename.c_str()); exit(1); } } inline void assure(std::ofstream& out, const std::string& filename = "") { using namespace std; if(!out) { fprintf(stderr, "Could not open file %s\n", filename.c_str()); exit(1); } } #endif // REQUIRE_H ///:~
Want to buy your Pack or Services from MandrakeSoft? Go to http://www.mandrakestore.com