I need to create two constructors, one of them by default and here the following question arises, both constructors must have the same name? For example:
CPartido::partidoVacio()
{
//Valor por defecto
m_puntosLocal = 0;
m_puntosVisitant = 0;
m_nomLocal = "????";
m_nomVisitant = "????";
m_puntsTotal = 0;
}
CPartido::partido(CCadena nomLocal, CCadena nomVisitant)
{
m_nomLocal = nomLocal;
m_nomVisitant = nomVisitant;
m_puntosLocal=0;
m_puntosVisitant=0;
m_puntosTotal = 0;
}
This would be correct?
Yes, but no, this slowly, the constructors have the same name as the class and are differentiated by the list of parameters, so when you call a ctor to create an object, by the list of parameters that it passes it knows which one to choose "yes everything is fine" .
Now, there are situations in which the parameters can be confusing to decide on one or the other, that is why there are named constructors -> "Named Constructors", take this with a grain of salt because I don't know if the standard catalogs them as ctor or like "static calls, static functions, or something else" that returns an object.
Now an example "is from a book" but it serves very well to explain why they exist and what they want to solve the "Named Constructors" and you could make use of these to have them identified by names, although in your case it is not ambiguous due to the amount of parameters.
To create a point you can do it by rectangular coordinates or by polar coordinates , in both cases you need two parameters, "same" to generate both objects but these are created in a different way, for example through polar coordinates , you use the radius and angle, meaning that internally the ctor will do different calculations.
That's why the overload would be ambiguous because which of them to choose if the parameter list is not different, this is what the named ctors solve.
Now static methods are called named constructors.
Now it's unambiguous, and this is when you can use it following that logic more or less to create ctor with the names you indicate.
the changes could be something like this pseudoode :
I would do something like this