dimanche 15 mai 2022

Default empty constractor

I am trying to create a very basic class with the default contructor:

    class Point {
    public:
          Point() = default;//con 1
          explicit Point(double x): x_axis(x), y_axis(0){}//con 2
          Point(const Point &other) = default;
          ~Point() = default;
    private:
           double x_axis;
           double y_axis;
    }

But when I try to use the default contructor in the main functtion it generates a random juck value for x_axis:

    Point p1;//generates random value
    Point p2{};//works as intended

so my question is, why is that? when I do the other contructor (con 2) like so:

    explicit Point(double x = 0): x_axis(x), y_axis(0){}

both of them work as intended. so technically I have 2 questions,

  1. why in the first try, no brackets generated a random value but {} worked, and in the second try they both worked
  2. what even is calling the default constructor with {}?

Aucun commentaire:

Enregistrer un commentaire