cibercitizen1 cibercitizen1 - 3 months ago 10
C++ Question

How to differentiate two constructors with the same parameters?

Suppose we want two constructors for a class representing complex numbers:

Complex (double re, double img) // construct from cartesian coordinates
Complex (double A, double w) // construct from polar coordinates


but the parameters (number and type) are the same: what is the more elegant way
to identify what is intended? Adding a third parameter to one of the constructors?

Answer

It is better to add static methods with appropriate names and let them to create the objects.

static Complex createFromCartesian(double re, double img);
static Complex createFromPolar(double A, double w);
Comments