# HG changeset patch # User Markus Bröker # Date 1239878953 -7200 # Node ID c064ce9f40f530a3bf4157090f06b4102efc2253 # Parent 6b3d7e3418c11545bc07c42c8843f53999b3f473 Vector and PMC ChangeLog * global var refCounter counts the objects * Copy Constructor implemented -> Surface cannot be copied ==> CC disabled -> Vector can be copied -> shows a warning committer: Markus Bröker diff --git a/pmc/cube.cc b/pmc/cube.cc --- a/pmc/cube.cc +++ b/pmc/cube.cc @@ -23,6 +23,8 @@ P[5] = new Vector (125, 25); P[6] = new Vector (125, 125); P[7] = new Vector (25, 125); + + refCounter++; } Cube::Cube (Surface * s, Vector & p1, Vector & p2, Vector & p3, Vector & p4, int h) @@ -39,9 +41,11 @@ P[3] = new Vector (p4); for (int i = 0; i < 4; i++) { - P[4 + i] = new Vector (*P[i]); + P[4 + i] = new Vector (); *P[4 + i] = *P[i] + location; } + + refCounter++; } Cube::Cube (Surface * s, Vector p[4], int h) @@ -57,6 +61,13 @@ P[4 + i] = new Vector (*P[i]); *P[4 + i] = *P[i] + location; } + + refCounter++; +} + +Cube::Cube (const Cube & copy) +{ + fprintf (stderr, "Copy Constructor in Cube disabled...\n"); } Cube::~Cube () diff --git a/pmc/include/cube.h b/pmc/include/cube.h --- a/pmc/include/cube.h +++ b/pmc/include/cube.h @@ -19,8 +19,9 @@ Cube (Surface *, algebra::Vector &, algebra::Vector &, algebra::Vector &, algebra::Vector &, int); Cube (Surface *, algebra::Vector[4], int); Cube (Surface *); + Cube (const Cube &); + virtual ~ Cube (); - virtual ~ Cube (); void show (); void move (algebra::Vector); }; diff --git a/pmc/include/drawable.h b/pmc/include/drawable.h --- a/pmc/include/drawable.h +++ b/pmc/include/drawable.h @@ -16,6 +16,7 @@ public: virtual ~ Drawable () { }; + virtual void move (algebra::Vector) = 0; virtual void show () = 0; }; diff --git a/pmc/include/object.h b/pmc/include/object.h --- a/pmc/include/object.h +++ b/pmc/include/object.h @@ -8,13 +8,17 @@ #include +extern int refCounter; + class Object { protected: std::string name; public: virtual ~ Object (); + virtual std::string getName (); virtual Object getClass (); + int getInstances (); }; #endif diff --git a/pmc/include/rectangle.h b/pmc/include/rectangle.h --- a/pmc/include/rectangle.h +++ b/pmc/include/rectangle.h @@ -18,7 +18,9 @@ Rectangle (Surface *, algebra::Vector & p1, algebra::Vector & p2, algebra::Vector & p3, algebra::Vector & p4); Rectangle (Surface *, algebra::Vector[4]); Rectangle (Surface *); + Rectangle (const Rectangle &); virtual ~ Rectangle (); + void move (algebra::Vector); void show (); }; diff --git a/pmc/include/surface.h b/pmc/include/surface.h --- a/pmc/include/surface.h +++ b/pmc/include/surface.h @@ -20,17 +20,14 @@ SDL_Surface *screen; public: - enum foregroundColor { BLACK=1, RED, GREEN, BLUE }; - Surface (int w, int h, int d); - ~Surface (); + enum foregroundColor { BLACK = 1, RED, GREEN, BLUE }; - int getWidth () { - return width; - }; + Surface (int w, int h, int d); + Surface (const Surface &) { /* Copy Constructor disabled */ }; + virtual ~Surface (); - int getHeight () { - return height; - }; + int getWidth () { return width; }; + int getHeight () { return height; }; void drawPixel (int x, int y); void drawLine (int x1, int y1, int x2, int y2); diff --git a/pmc/include/vector.h b/pmc/include/vector.h --- a/pmc/include/vector.h +++ b/pmc/include/vector.h @@ -14,6 +14,7 @@ enum Mode { DEG, RAD, GRAD }; Vector (int xx = 0, int yy = 0); + Vector (const Vector &); virtual ~ Vector (); int X (); @@ -29,6 +30,7 @@ private: Mode mode; + int x; int y; }; diff --git a/pmc/main.cc b/pmc/main.cc --- a/pmc/main.cc +++ b/pmc/main.cc @@ -91,6 +91,11 @@ for (i = 0; i < MAX; i++) d[i]->move ((Vector (x + STEP, y))); break; + + case SDLK_RETURN: + fprintf (stderr, "Objects remaining: %d\n", surface->getInstances ()); + break; + default: break; } @@ -108,5 +113,10 @@ delete surface; + /* + * the remaining objects will be removed on exit + */ + fprintf (stderr, "Objects remaining on exit: %d\n", refCounter); + return EXIT_SUCCESS; } diff --git a/pmc/object.cc b/pmc/object.cc --- a/pmc/object.cc +++ b/pmc/object.cc @@ -7,11 +7,14 @@ #include +int refCounter = 0; + Object::~Object () { #ifdef DEBUG std::cerr << "Freeing Object " << name << std::endl; #endif + refCounter--; } std::string Object::getName () @@ -23,3 +26,8 @@ { return *this; } + +int Object::getInstances () +{ + return refCounter; +} diff --git a/pmc/rectangle.cc b/pmc/rectangle.cc --- a/pmc/rectangle.cc +++ b/pmc/rectangle.cc @@ -19,6 +19,8 @@ P[1] = new Vector (0, 100); P[2] = new Vector (100, 100); P[3] = new Vector (0, 100); + + refCounter++; } Rectangle::Rectangle (Surface * s, Vector & p1, Vector & p2, Vector & p3, Vector & p4) @@ -32,6 +34,8 @@ P[1] = new Vector (p2); P[2] = new Vector (p3); P[3] = new Vector (p4); + + refCounter++; } Rectangle::Rectangle (Surface * s, Vector p[4]) @@ -44,6 +48,13 @@ for (int i = 0; i < 4; i++) { P[i] = new Vector (p[i]); } + + refCounter++; +} + +Rectangle::Rectangle (const Rectangle & copy) +{ + fprintf (stderr, "Copy Constructor in Rectangle disabled...\n"); } Rectangle::~Rectangle () diff --git a/pmc/surface.cc b/pmc/surface.cc --- a/pmc/surface.cc +++ b/pmc/surface.cc @@ -25,10 +25,13 @@ color = red; bpp = screen->format->BytesPerPixel; + + refCounter++; } Surface::~Surface () { + SDL_FreeSurface (screen); SDL_Quit (); } diff --git a/pmc/vector.cc b/pmc/vector.cc --- a/pmc/vector.cc +++ b/pmc/vector.cc @@ -17,11 +17,22 @@ y = yy; mode = RAD; + refCounter++; + #ifdef DEBUG fprintf (stderr, "New Vector: (%.3d, %.3d)\n", x, y); #endif } +Vector::Vector (const Vector & copy) +:x (copy.x), y (copy.y) +{ + fprintf (stderr, "Warning: Copy Constructor: "); + vector (); + fprintf (stderr, "\n"); + refCounter++; +} + Vector::~Vector () { #ifdef DEBUG diff --git a/vector.cc b/vector.cc --- 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; }