mercredi 14 novembre 2018

how to copy constructor pointer vector c++

I'm trying to create a copy constructor for a class "Table" with the following fields:

private:
    int id ;
    int capacity;
    bool open;
    std::vector<Customer*> customersList;
    std::vector<OrderPair> orderList;

I want to make a deep copy of all the fields and I've written the following code and I'm not sure I did a deep copy on the customersList since its a pointers vector. Can someone tell me if I made a deep or sallow copy? Thank you in advance.

the code:

Table(const Table& t): capacity(t.getCapacity()), open(t.isOpen()), id(t.getId()){
    std::vector<Customer*> tmp(t.customersList);
    customersList = tmp;
}

Or maybe I over copied and I should only do this? :

Table(const Table& t): customersList(t.customersList), capacity(t.getCapacity()), 
open(t.isOpen()), id(t.getId()){}

Thanks again!

Aucun commentaire:

Enregistrer un commentaire