C ++ Template or + operator to create vector from pointer

I have a specific base class with pure virtual functions and I would like to know if it is possible to implement the function below readFromFile()

like this:

class Model {
    time_t changedateTime;

    virtual void writeToFile(std::string fileName, Model* model) = 0;

    // Is that possible, a vector of its own class pointer
    virtual std::vector<Model*> readFromFile(std::string fileName) = 0; 

}

      

Real implementations of the model:

class Customer : public Model {
    std::string Name;
    std::string Address;
}

      

or

class OrderItem : public Model {
    std::string Item;
    std::string Price;
}

      

And how to write to a file and read to a file:

void Model::writeToFile(std::string fileName, Model* model)
{
    // .... opens the file....

    // ... append model to the end of file....

    // ... close file...
}

std::vector(Model*) Model::readFromFile(std::string fileName, Model* model)
{
    // .... opens the file fileName...

    // ...get several lines of data to add to returning vector...

    std::vector(Model*) returnVector;

    Model* newModel = new Something // <--- How can I create here a new class of
                                    // the type I want to add to the vector??????

}

      

I am stuck here when creating a new object from an inherited model type to add it to the returned vector ( returnVector

).

I don't know if the solution will work in an + operator

in-class Model

or with C ++ template

or even something else. I came from C # and there I would use a <T>

generic type here easily.

I actually need help moving forward and really appreciate the expert comment.

+3


source to share


2 answers


Since you want to Model

be a generic base class, templates don't really fit. You need to teach the class to create new objects of its type. In design pattern terminology, you need a factory method:

class Model {
   time_t changedateTime;

   virtual void writeToFile(std::string fileName, Model* model);
   virtual std::vector<Model*> readFromFile(std::string fileName);

   virtual Model* createNewObject() const = 0;
}

std::vector(Model*) Model::readFromFile(std::string fileName, Model* model)
{
   //.... opens the file fileName...

   //...get several lines of data to add to returning vector...

   std::vector<Model*> returnVector;

   Model* newModel = createNewObject();

   // proceed normally

}


Model* Customer::createNewObject() const
{
  return new Customer;
}

      




A few notes:

  • You shouldn't use raw pointers that own things. Use std::unique_ptr

    or another suitable smart pointer.

  • It's not entirely clear why readFromFile

    that which returns a lot Model

    (in a vector) is a member Model

    . Is the model hierarchical in some way?

+2


source


It is not clear as to the question. The standard approach to creating an object in such cases is to implement an "Abstract Factory". You will still need to figure out how to decide which object to create. All that says there are libraries to provide the functionality you are looking for.



-2


source







All Articles