c++ - Store a variable number of objects of a variable number of derived class and deep copy them -


i'm bit stumped how achieve in c++. have feeling might have architecture.
let's have a garage in montreal , in garage, there different cars.i go vector of pointers base class car here. here problem though, want deep copy garage , open 1 in miami. futhermore, saab cars needs have different copy constructor, same signature. there other exceptions down road. don't know what's in garage beforehand. here code have far.

        //base class         class car         {             public:                  car(int speed);              private:                 int speed;          }         class garage         {                public:                       garage(){};                       //copy constructor of garage                       garage(const garage &tocopy)                       {                          for(int i=0;i<tocopy.cars.size();i++)                          {                          receivecar(*cars[i]);                          }                       }                       vector<std::shared<car>> cars;                       template <typename t>                       t* receivecar()                       {                          std::shared_ptr<t> ptr = std::make_shared<t>();                          cars.push_back(ptr);                          return ptr.get();                       }                       t* receivecar(car &tocopy)                       {                          std::shared_ptr<t> ptr = std::make_shared<t>(tocopy)                     cars.push_back(ptr);                          return ptr.get();                        }              }       class saab : public car     {         public:               saab(int speed)                 :car(speed)               {                  dowork();               };     }     class astonmartin: public car     {         public:               astonmartin(int speed)                   :car(speed)               {                  doimportantstuff();               };               astonmartin(const astonmartin& tocopy)                   :car(tocopy.speed)               {                  doimportantstuff();               };     }   

of course, copy constructor used implicit 1 car. possible call derived class' 1 instead without dynamic casting between ifs/elses every exception rule there is. maybe store each derived classes pointer somehow?
ps:i know should not returning raw pointers, destructors cars private in code, shared_pointers still safe.

in context of inheritance copy constructor should designed called “virtual copy constructor”. these functions called copyself, cloneself or clone.

here elaborated example using copy constructors , virtual copy constructor:

#include <memory> #include <iostream> #include <vector> using std::cout; using std::endl; using std::vector;  //base class class car { public:     car(int speed): speed(speed) {};     car(const car& c): speed(c.speed)     { cout <<"copy constructor car" <<endl;     }     virtual void display()     { cout <<" speed: " <<speed <<endl;     }     virtual car* clone()const =0;  // virtual copy constructor   private:     int speed; }; typedef std::shared_ptr<car> carp; class garage { private:     vector<carp> cars;   public:     garage() {}     //copy constructor of garage     garage(const garage &tocopy)     { cout <<"copy constructor garage" <<endl;       (carp c : tocopy.cars)       { carp cp(c->clone());         receivecar(cp);       }     }     void receivecar(carp tocopy)     { cars.push_back(tocopy);     }     void display()     { (carp c : cars)  c->display();     } }; class saab: public car { public:     saab(int speed): car(speed)     { cout <<"constructor saab" <<endl;     }     void display()     { cout <<"car: saab";  car::display();     }     saab* clone()const     { return new saab(*this);     } }; class astonmartin: public car { public:     astonmartin(int speed): car(speed)     { cout <<"constructor astonmartin" <<endl;     }     astonmartin(const astonmartin& tocopy): car(tocopy)     { cout <<"copy constructor astonmartin" <<endl;     }     void display()     { cout <<"car: astonmartin";  car::display();     }     astonmartin* clone()const     { return new astonmartin(*this);     } }; int main() {   carp saab  =std::make_shared <saab>(100);   carp aston =std::make_shared <astonmartin>(200);   garage montreal;   montreal.receivecar(saab);   montreal.receivecar(aston);   montreal.display();   garage miami(montreal);  // decision offer user copy constructor:   miami.display(); } 

produced output:

constructor saab constructor astonmartin car: saab speed: 100 car: astonmartin speed: 200 copy constructor garage copy constructor car copy constructor car copy constructor astonmartin car: saab speed: 100 car: astonmartin speed: 200 

Comments