Doubt with the builders

2

I need to create two constructors one of them by default and here I have the following doubt, 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?

    
asked by ElPatrón 14.03.2017 в 00:55
source

2 answers

2
  

OP comment, I think it is useful to understand this answer in addition to what you mention in the question:

     

question + ... in an activity they ask me to make two constructors, one of which is by default and one of them must be called partidoVacio and   another party ...

Yes, but not, 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, from the list of parameters that you know what to choose "if all is well" .

Now, there are situations in which the parameters can be confusing to decide on one or the other, that is why there are the named builders - > "Named Constructors", takes this with tweezers because I do not know if the standard catalogs them as ctor or as "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 that they want to solve the "Named Constructors" and you could make use of them to have them identified by names, even if it is not in your case ambiguous for the number of parameters.

class Point {
public:
  Point(float x, float y);     // Rectangular coor
  Point(float r, float a);     // Polar coor

};

int main()
{
  Point p = Point(5.7, 1.2);   
}

To create a point you can do it by coordinates rectogular or by polar coordinates , in both cases you need two parameters, "equal" to generate both objects but these they are created differently, for example through polar coordinates , you use the radius and angle, so internally the ctor will make different calculations.

That's why the overload would be ambiguous because which one of them to choose if the list of parameters is not different, this is what solves the named ctor.

#include <cmath>               

class Point {
public:

  static Point rectangular (float x, float y);      // Rectangular 
  static Point polar (float radius, float angle);   // Polar 

private:

  Point(float x, float y);     // Rectangular 
  float x_, y_;
};

inline Point::Point(float x, float y)
  : x_(x), y_(y) { }

inline Point Point::rectangular(float x, float y)
{ 
    return Point(x, y); 
}

inline Point Point::polar(float radius, float angle)
{
    return Point(radius*std::cos(angle), radius*std::sin(angle)); 
}

Now the static methods are called builders named "named constructors".

int main()
{
  Point point1 = Point::rectangular(5.7, 1.2);   // Pectangular
  Point point2 = Point::polar(5.7, 1.2);         // Polar
}

Now it is not ambiguous, and it is here when you can use it following that logic more or less to create ctor with the names that you indicate.

the changes could be something like this pseudoode :

//..
private:

      CPartido(CCadena ..., CCadena ...);     // Rectangular 

//..
inline CPartido::CPartido(CCadena ..., CCadena ...)
{ 
//Valor por defecto
m_puntosLocal = 0;
m_puntosVisitant = 0;
m_nomLocal = ...;
m_nomVisitant = ...;
m_puntsTotal = 0;
}

inline CPartido CPartido::partidoVacio()
{ 
    return CPartido("????", "????"); 
}

inline CPartido CPartido::partido(CCadena nomLocal, CCadena nomVisitant)
{
    return return CPartido(nomLocal, nomVisitant);
}
int main()
{
  CPartido partido1 = CPartido::partido(stack, overflow)
  CPartido partido2 = CPartido::partidoVacio()
}
    
answered by 14.03.2017 / 02:16
source
0

I would do something like this

#include <iostream>
using namespace std;

class CPartido
{
    private:
       int m_puntosLocal;
       int m_puntosVisitant;
       ....
    public:
       // Constructor with no arguments
       CPartido(): m_puntosLocal(0), m_puntosVisitant(0) { }

       // Constructor with two arguments
       CPartido(int nomLocal, int nomVisitant): m_puntosLocal(nomLocal), m_puntosVisitant(0){ }

       // Function
       int CPartidoPartido() {  return (m_puntosLocal* m_puntosVisitant);  }

...
    
answered by 14.03.2017 в 01:02