Double-Bound Driver Without Copying

This driver uses the double-bound array class, but avoids copying and assigning objects.

/* * This is a simple driver for the bound array class that does not copy. */ #include <cstdlib> #include <string> #include <iostream> using std::string; // This is a dirty trick that allows me to compile with different bounded // array headers. Normally uses boundarr1.h, but add compile flag // -DBAHDR=\"somethingelse.h\" to use another. Note that this procedure // depends on the compiler used, but they will probably be pretty consistent // for this. How you add the flag depends on how you are running the // compiler. If an IDE, you'll have to much through its menus. #ifndef BAHDR #define BAHDR "boundarr1.h" #endif #include BAHDR /* * This function takes a BoundArr by value by reference and increments each * item by the given amount, and prints it before and after. */ void arrinc(string label, BoundArr<int> &arr, int incr) { std::cout << " " << label << " before: " << arr << std::endl; for(int i = arr.low(); i <= arr.high(); ++i) arr.at(i) += incr; std::cout << " " << label << " + " << incr << ": " << arr << std::endl; } int main() { std::cout << "Using the version in " << BAHDR << std::endl << std::endl; // Make two arrays and fill them. The numbers have no // particular meaning. BoundArr<int> bai1(5,10); for(int i = bai1.low(); i <= bai1.high(); ++i) bai1.at(i) = 5 + i/6 + 2*i - i*i/5; BoundArr<int> bai2(-4,8); for(int i = bai2.low(); i <= bai2.high(); ++i) bai2.at(i) = 4 + 17*bai1.at(bai1.low() + (i - bai2.low()) % bai1.size())/7; std::cout << "Two arrays with a bunch of useless numbers:" << std::endl; std::cout << " bai1: " << bai1 << std::endl; std::cout << " bai2: " << bai2 << std::endl << std::endl; // Call the exciting function a couple of times. std::cout << "Using a function to increment copies of each array:" << std::endl; arrinc("bai1", bai1, 8); arrinc("bai2", bai2, -5); std::cout << std::endl; // The arrays were passed to the incrementer by reference, so are // they will have been incremented. std::cout << "Since references were used, we see changes in main: " << std::endl; std::cout << " bai1: " << bai1 << std::endl; std::cout << " bai2: " << bai2 << std::endl << std::endl; // Make changes to each. for(int i = bai1.low()+1; i <= bai1.high()-1; ++i) bai1.at(i) = 0; for(int i = bai2.low()+2; i <= bai2.high()-3; ++i) bai2.at(i) = 99; // And independent copies. std::cout << "So we change some in the middle just 'cause we can." << std::endl; std::cout << " bai1: " << bai1 << std::endl; std::cout << " bai2: " << bai2 << std::endl; }