Bruce Eckel's Thinking in C++, 2nd Ed | Contents | Prev | Next |
//: C07:Stash4.h // Function overloading #ifndef STASH4_H #define STASH4_H class Stash { int size; // Size of each space int quantity; // Number of storage spaces int next; // Next empty space // Dynamically allocated array of bytes: unsigned char* storage; void inflate(int increase); public: Stash(int size); // Zero quantity Stash(int size, int initQuant); ~Stash(); int add(void* element); void* fetch(int index); int count(); }; #endif // STASH4_H ///:~
//: C07:Stash4.cpp {O} // Function overloading #include <cstdlib> #include <cstring> #include <cstdio> #include "../require.h" #include "Stash4.h" using namespace std; Stash::Stash(int sz) { size = sz; quantity = 0; next = 0; storage = 0; } Stash::Stash(int sz, int initQuant) { size = sz; quantity = 0; next = 0; storage = 0; inflate(initQuant); } Stash::~Stash() { if(storage) { puts("freeing storage"); free(storage); } } int Stash::add(void* element) { if(next >= quantity) // Enough space left? inflate(100); // Add space for 100 elements // Copy element into storage, // starting at next empty space: memcpy(&(storage[next * size]), element, size); next++; return(next - 1); // Index number } void* Stash::fetch(int index) { if(index >= next || index < 0) return 0; // Not out of bounds? // Produce pointer to desired element: return &(storage[index * size]); } int Stash::count() { return next; // Number of elements in Stash } void Stash::inflate(int increase) { void* v = realloc(storage, (quantity+increase)*size); require(v); // Was it successful? storage = (unsigned char*)v; quantity += increase; } ///:~
//: C07:Stshtst4.cpp //{L} Stash4 // Function overloading #include <cstdio> #include "../require.h" #include "Stash4.h" using namespace std; #define BUFSIZE 80 int main() { int i; FILE* file; char buf[BUFSIZE]; char* cp; // .... Stash intStash(sizeof(int)); for(i = 0; i < 100; i++) intStash.add(&i); file = fopen("STSHTST4.CPP", "r"); require(file); // Holds 80-character strings: Stash stringStash(sizeof(char) * BUFSIZE); while(fgets(buf, BUFSIZE, file)) stringStash.add(buf); fclose(file); for(i = 0; i < intStash.count(); i++) printf("intStash.fetch(%d) = %d\n", i, *(int*)intStash.fetch(i)); i = 0; while( (cp = (char*)stringStash.fetch(i++)) != 0) printf("stringStash.fetch(%d) = %s", i - 1, cp); putchar('\n'); } ///:~