Destructor help

This is a discussion on Destructor help within the C++ Programming forums, part of the General Programming Boards category; Hello, I am just learning c++ and I'm trying to create heap-based objects. In the destructor of the class, I ...

  1. #1
    Unregistered
    Guest

    Destructor help

    Hello,

    I am just learning c++ and I'm trying to create heap-based objects. In the destructor of the class, I must delete the object from the heap. My question is: How can I delete the heap objects that I put into a vector?

    For example: this is a Sample class. This class has a vector called myVector and Testing is another object.

    Code:
    //File: Sample.cc
    #include <string>
    #include <vector>
    #include "Testing.h"
    
    using namespace std;
    
    Sample::Sample() {}
    Sample::~Sample() {
    	for (int i=0; i<myVector.size(); i++)
    	{	delete myVector[i];
    	}
    }
    
    void Sample::sampleMethod(string myValue) {
         Testing *t;       //create an new Testing object, t, on the heap
         t = new Testing();
    
         t->setValue(myValue);  //set the value of t
         myVector.push_back(*t);  //put t into a vector 
    }
    I get this error while compiling:
    Sample.cc: In method `Sample::~Sample()':
    Sample.cc:11: type `class Testing' argument given to `delete', expected pointer


    Does anyone know what is wrong?
    How can I delete the objects in the vector?

    Thanks alot!

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Right now, myVector is declared like this.

    Code:
    std::vector<Testing> myVector;
    And this segment of code is not doing what you expected..

    Code:
    void Sample::sampleMethod(string myValue) {
         Testing *t;       //create an new Testing object, t, on the heap
         t = new Testing();
    
         t->setValue(myValue);  //set the value of t
         myVector.push_back(*t);  //put COPY OF VALUE POINTED BY t into a vector 
    }  // and t is permenantly leaked here
    Value types cannot be deleted (or perhaps they can, but it requires operator delete overloaded.. and I see no reason to do it). That probably confusing note aside, if you are going to use dynamic memory with the vector, you need to declared a vector of pointers, not a vector of value types.

    The code then becomes

    Code:
    // in class declaration
    std::vector<Testing*> myVector;
    
    void Sample::sampleMethod(string myValue) {
         Testing *t;       //create an new Testing object, t, on the heap
         t = new Testing();
    
         t->setValue(myValue);  //set the value of t
         myVector.push_back(t);  //put t into a vector 
    }  
    
    // and your destructor works as expected

    It should be fine after you do that.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537

    A better way!

    Push the pointer onto the vector. This is better because it will not create a copied object (which I think will happend if you dereference the pointer), and operations are quicker when performed on pointer rather than complete objects.

    Code:
    void Sample::sampleMethod(string myValue) {
         Testing *t;       //create an new Testing object, t, on the heap
         t = new Testing();
    
         t->setValue(myValue);  //set the value of t
         myVector.push_back(t);  //put pointer t into a vector 
    }
    Then the destructor should works as you originally tried (hopefully )

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21