dimanche 22 août 2021

Why is object initialization different inside a struct?

I have created a class Point, here is the corresponding hpp file.

#ifndef POINT
#define POINT
class Point
{
 protected:
 int x;
 int y;
 public:
 Point(int x = 10, int y = 10);
 void movePoint(int moveX, int moveY);
 void printCoordinates();
};
#endif

I noticed that in the main, I can declare an object and initialize it this way:

Point myPoint(1, 1);

If I want to create a structure containing two points, it won't let me initialize it this way, instead, I have to use curly brackets, this way:

struct segment
{
 Point point1 = {0, 0};
 Point point2 = {15, 15};
};

Why is that?

Aucun commentaire:

Enregistrer un commentaire