Function that receives as parameter a C ++ template

0

I'm doing a project in C ++ and I have a class called DataContext where I will have vectors that store objects of another class as member data. I want to perform a function that resieves a vector of any type of class to be able to add, search, delete or modify the objects belonging to said vector.

class DataContext
{
    private:
        vector<TarjetaCredito> listaTarjetas = vector<TarjetaCredito>();
        vector<Contrato> lvector<Contrato> listaContrato = vector<Contrato>();
    public:
        DataContext();
        virtual ~DataContext();
        static bool Create(vector<vector_de _cualquier_clase>);//METODO QUE QUIERO IMPLIMENTAR.
};

I thought that when passing the vector as an argument, where does the data type of the vector have to put a template, if that is correct, where would it be?

I hope you can help me, thank you very much.

    
asked by Leonardo Arellano 11.06.2018 в 23:34
source

1 answer

1

Since the template is at the level of the member function, that is, it does not affect the class, it is normal to leave that function as a template only:

class DataContext
{
  private:
    vector<TarjetaCredito> listaTarjetas = vector<TarjetaCredito>();
    vector<Contrato> lvector<Contrato> listaContrato = vector<Contrato>();
  public:
    DataContext();
    virtual ~DataContext();

    template<class T>
    static bool Create(vector<T> &);
};

Since you mention that one of the objectives of the function is to modify the vector, it becomes a necessity that the function receives a reference to said vector.

    
answered by 12.06.2018 в 07:45