Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ Template or + operator to create a vector from a pointer

Tags:

c++

templates

I have a defined base class with pure virtual functions and I would like to know if its possible to implement the readFromFile() below function in the following way:

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 Model:

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

or

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

And than the write to file and read to file implementation:

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´m stucked here at the creation of a new object from the inherited model type to add it to the vector to return (returnVector).

I don´t know if the solution will come defing a + operator on the Model class or using C++ template, or even something else.. I´m coming from C# and there I would be using the <T> generic type easily here.

In fact, I need help to move further and appreciate very much an expert comment.

like image 280
Mendes Avatar asked May 06 '26 14:05

Mendes


1 Answers

Since you want Model to be a common base class, templates are not really the way to go. You need to teach the class to make new objects of its own 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 side notes:

  • You shouldn't be using raw pointers which own things—use std::unique_ptr or another suitable smart pointer instead.

  • It's not quite clear why readFromFile, which returns many Models (in a vector), is a member of Model. Is the model hierarchical in some way?

like image 160
Angew is no longer proud of SO Avatar answered May 08 '26 04:05

Angew is no longer proud of SO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!