Let's say there's a struct
type that will be used used to hold the coordinates of a Point in 3D space.
This could be defined like:
struct Point { double x, y, z; };
Making use of double
values because we want to be as precise as possible.
An instance of this struct
, could be declared using designated compound literals, like:
double x = 0.0;
double y = 0.0;
double z = 0.0;
...
Point p = (struct Point){ .x=x, .y=y, .z=z };
However, when trying to declare another Point starting from different types:
int x = 0;
int y = 0;
int z = 0;
...
Point p = (struct Point){ .x=x, .y=y, .z=z };
The compiler throws an error, because it's unable to find a suitable constructor. This works if one does:
Point p = (struct Point){ .x=(double)x, .y=(double)y, .z=(double)z };
Is there a way to overload the Point constructor, so that one does not have to manually cast each member of the initializer list to double?
Perhaps using something like Point(initializer_list<int> ...) {}
in the struct
's declaration?