ssd
ssd

Reputation: 2432

Why constructor calling constructor creates weird results?

I'm just testing a code shown below (learning oop in c++).

(system: linux, gcc compiler with -std=c++11)

#include <iostream>

class Point {
  private:
    double x, y;

  public:
    static unsigned int pCount;

    // constructor1 with no initial arguments
    Point() {
      x = 0.0f;
      y = 0.0f;
      std::cout << "Point()" << " x = " << x << " y = " << y
          << " point no: " << ++pCount << std::endl;
    }

    /*
    // constructor1 alternative: calling another constructor
    Point() {
      Point(0.0f, 0.0f);
      std::cout << "Point()" << std::endl;
    }
    */

    // constructor2 with initial arguments
    Point(double cx, double cy) {
      x = cx;
      y = cy;
      std::cout << "Point(double, double)" << " x = " << x << " y = " << y
          << " point no: " << ++pCount << std::endl;
    }

    void Show() {
      std::cout << "Show()" << " x = " << x << " y = " << y << std::endl;
    }

    virtual ~Point() {
      std::cout << "~Point()" << " x = " << x << " y = " << y 
          << " point no: " << pCount-- << std::endl;
    }
};

unsigned int Point::pCount = 0;

int main(int argc, char *argv[]) {
  Point p1(2.5, 7.6);
  Point p2;

  p2.Show();
  p1.Show();

  return (0);
}

The code above creates the output as:

Point(double, double) x = 2.5 y = 7.6 point no: 1
Point() x = 0 y = 0 point no: 2
Show() x = 0 y = 0
Show() x = 2.5 y = 7.6
~Point() x = 0 y = 0 point no: 2
~Point() x = 2.5 y = 7.6 point no: 1

However, when I comment-out the constructor1 and uncomment constructor1 alternative, the output created gets a little weird as below:

Point(double, double) x = 2.5 y = 7.6 point no: 1
Point(double, double) x = 0 y = 0 point no: 2
~Point() x = 0 y = 0 point no: 2
Point()
Show() x = 4.64944e-310 y = 9.88131e-324
Show() x = 2.5 y = 7.6
~Point() x = 4.64944e-310 y = 9.88131e-324 point no: 1
~Point() x = 2.5 y = 7.6 point no: 0

I think in the latter case (constructor1 alternative), a temporary object is created and then its pointer is passed to p2. Anyway, so far, so good... But I'm stuck with the values assigned to the variables x & y: The former shows x = 0, y = 0 as expected; however the latter spits out x = 4.63813e-310, y = 9.88131e-324, though values are very close to zero, it still seems weird to me.

Why the latter case does not assign the exact "zero" values?

Upvotes: 0

Views: 88

Answers (1)

lubgr
lubgr

Reputation: 38267

I think in the latter case (constructor1 alternative), a temporary object is created and then its pointer is passed to p2

You're right, a temporary is indeed constructed, but it's destroyed right after construction. You might want to use a delegate constructor like this:

Point() : Point(0.0, 0.0) {}

This will call the constructor with two double arguments, and both data members are initialized. Note that the "weird" behavior you were referring to is nothing but uninitialized data - the data members contain garbage values, as they haven't been initialized.

Just as a side note (I realize that you said it's about learning OOP, so don't take that remark too seriously): it seems to me that the two coordinate data members of your Point class can vary independently. Your class hence doens't manage an invariant - in that case, you might want to consider to simplify the type to

struct Point { double x = 0.0; double y = 0.0; };

which allows for default construction

Point p; // both x and y are 0.0 now

as well as aggregate initialization

Point p{3.14, 42.0};

Additional functionality that is part of the interface of your class (printing, distance to another point etc.) can conveniently be defined within free functions.

Upvotes: 2

Related Questions