vector.cc
changeset 54 c064ce9f40f5
parent 45 7197576fedcf
child 74 829976007e62
--- a/vector.cc
+++ b/vector.cc
@@ -10,6 +10,8 @@
 #define GETRANDOM(max) (1+(int)((float)max*rand()/RAND_MAX+1.0))
 #define MAX 6
 
+static int refCounter = 0;
+
 namespace algebra {
     class Vector {
       private:
@@ -18,6 +20,7 @@
 
       public:
         Vector (int xx = 0, int yy = 0);
+        Vector (const Vector &);
        ~Vector ();
 
         int X () { return x; };
@@ -33,10 +36,19 @@
     Vector::Vector (int xx, int yy) {
         x = xx;
         y = yy;
+
+        refCounter++;
+    }
+
+    Vector::Vector (const Vector & copy)
+    : x (copy.x), y (copy.y) {
+        fprintf (stderr, "Warning Copy Constructor\n");
+        refCounter++;
     }
 
     Vector::~Vector () {
         fprintf (stderr, "Removing Vector (%.3d, %.3d)\n", x, y);
+        refCounter--;
     }
 
     Vector Vector::operator+ (Vector & v) {
@@ -60,7 +72,7 @@
 
 int main (int argc, char **argv)
 {
-    Vector *v[MAX];
+    Vector **v;
     Vector result;
 
     srand (time (NULL));
@@ -70,6 +82,8 @@
         return EXIT_FAILURE;
     }
 
+    v = new Vector *[MAX];
+
     for (int i = 0; i < MAX; i++) {
         v[i] = new Vector (GETRANDOM (100), GETRANDOM (100));
     }
@@ -96,10 +110,12 @@
         printf (" = %3.2f\n", result.abs ());
     }
 
-    printf ("\nCLEANUP\n");
-
     for (int i = 0; i < MAX; i++)
         delete v[i];
 
+    delete [] v;
+
+    fprintf (stderr, "Objects remaining on exit: %d\n", refCounter);
+
     return EXIT_SUCCESS;
 }