How to make an arrangement of objects within another class? c ++

1

I need to make an arrangement of size 10 objects within a different class, The only way I know how to do it is this:

(B is another class)

     class A
{
private:
    B arreglo[10];
public:
    A();
    A(string, int, B arreglo[10]);
};

The problem is I'm not entirely clear what the constructor should be by reference, how should I pass the fix to the constructor?

Could someone explain to me how? ...

Is there any way to handle it with pointers? ...

    
asked by Javier Saavedra 19.09.2018 в 20:30
source

1 answer

2
  

The problem is I'm not entirely clear what the constructor should be by reference, how should I pass the fix to the constructor?

The constructor by reference has this signature:

A(const A &referencia);

So your function:

A(string, int, B arreglo[10]);

It is NOT a constructor by reference. The fix is passed to the constructor when passing a reference to object A , said reference contains the arrangement with which you want to work. So, your constructor by reference could look something like this:

A::A(const A &referencia)
{
    std::copy(std::begin(referencia.arreglo), std::end(referencia.arreglo), std::begin(arreglo));
}

In the previous code, we copy the content of the arreglo belonging to the referencia in the referencia belonging to the object that is being built.

    
answered by 20.09.2018 в 07:49