cibercitizen1 cibercitizen1 - 1 year ago 68
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 Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download