0

I have been trying multiple things and thinking for a while, but I just can't think of the correct syntax/logic for it.

I have a base class and two derived classes. I am using dynamic binding to make a vector which stores instances of all 3 classes. Then, I am reading in from a file, it specifies which class it belongs to (I will use an if statement to check the string in the file, e.g. "base", "der1", "der2"). It will then push that onto the stack.

I can manage the above if there is only one of each class, however, there are multiples of each one. Therefore, something like the below code wont work:

vector<Base*> myVec;

Then:

Base *b = new Base;
Der1 *d1 = new Der1;
Der2 *d2 = new Der2;

//read the file and fill in the classes data members

myVec.push_back(b);
myVec.push_back(d1);
myVec.push_back(d2);

The above will just read each type of class once each and push them on. How would I implement something along the lines of:

for(int i = 0; i < lines; i++) //lines = how many lines in file
{
    cin.get(whatType, ':'); //reads a string up to the delim char :

    if(whatType == "Base")
    {
        //read line and fill rest of data members...
        myVec.push_back(b);
    }
    else if(whatType == "Der1")
    {
        //read line and fill rest of data members...
        myVec.push_back(d1);
    }
    if(whatType == "Der2")
    {
        //read line and fill rest of data members...
        myVec.push_back(d2);
    }
}

However, when the same class type is read in again, the previous one will be overwritten as well as it's a pointer to one instance? So outputting at the end will be incorrect. I want them all to be unique instances.

How would I go about doing that? I have no clue.

0

1 Answer 1

3

You should create new instance of class every time, like this:

    vector<Base*> myVec;

    // main loop
    for (int i = 0; i < lines; i++) //lines = how many lines in file
    {
      cin.get(whatType, ':'); //reads a string up to the delim char :

      if(whatType == "Base")
      {
        Base *b = new Base;
        //read line and fill rest of data members...
        myVec.push_back(b);
      }
      else if(whatType == "Der1")
      {
        Der1 *d1 = new Der1;
        //read line and fill rest of data members...
        myVec.push_back(d1);
      }
      if(whatType == "Der2")
      {
        Der2 *d2 = new Der2;
        //read line and fill rest of data members...
        myVec.push_back(d2);
      }
    }

    // deleting pointers
    for (int i = 0; i < myVec.size(); ++i)
    {
      delete myVec[i];
    }
3
  • Thank you. That was simpler than I expected. Each time the same class type is read, the same pointer points to a new object, allowing you to push back multiple instances of it, and they will all remain in the vector the way they were pushed in? Then I would just need to delete each pointer once (3 deletes) at the end of my program to prevent memory leaks? Sep 26, 2014 at 6:56
  • Yes. You can create pointers and initialize it just in your loop, you don't need to declare pointers before the loop. The main thing is creating new instance of object on every step of the loop. Also, you have to delete each pointer in your container to prevent memory leaks.
    – Alex
    Sep 26, 2014 at 9:08
  • Thank you, but I'm most probably not allowed to use auto_ptr. I have calculation functions and a print() function after the for() loop finishes. If I delete the pointer inside the if statements, the outer functions don't work as desired. Also, I am unable to delete the pointers after the outer functions because they weren't defined outside. In this case, is it impossible to delete the pointers? Sep 26, 2014 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.