-1

I'm trying to delete my pointer to a vector but it includes pointers (in a way)

struct TestObject
{
    public:
    // some values
};

template <typename T> class VectorObject
{
    public:
    T       Object;
};

vector< VectorObject<TestObject*> > *pVector = 
                                    new vector< VectorObject<TestObject*> >();

TestObject *test = new TestObject;

VectorObject<TestObject*> testObject;
testObject.Object = test;

pVector->push_back(testObject);

vector<VectorObject<TestObject*>>::iterator i;

for (i = pVector->begin(); i != pVector->end(); i++)
{
     delete * (i->Object);
}

delete pVector;

Isn't this exactly the way it should work? Or do I have to change to:

vector< VectorObject < TestObject > *>?

Requirements

TestObject have to be pointer, because it's returned from an external Library method.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Deukalion
  • 2,516
  • 9
  • 32
  • 50

1 Answers1

1

This is what your code should look like:

vector< VectorObject<TestObject> > pVector;

VectorObject<TestObject> testObject;

pVector.push_back(testObject);

There is no need to use pointers.


If you're receiving pointers from an external method that you're expected to free, you should use unique_ptr to manage their lifetime:

vector< unique_ptr<TestObject> > pVector;
unique_ptr<TestObject> test(external_method());
pVector.push_back(test);
ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • Since TestObject can be pointer whether I like it or not, this is just an example. TestObject is just a "TestObject", what really gets stored is a pointer that I get returned from another method (out of my control). – Deukalion Aug 22 '12 at 13:59
  • @Deukalion, lack of explicit pointer usage in userspace C++ application is usually signature of good programmer - manual memory management is error-prone and can get complicated; it's better to rely on standard library classes that manage memory automatically and on RAII. – Griwes Aug 22 '12 at 13:59
  • 3
    If you need it to be a pointer *you need to explain why*. You can't expect people to guess what your undisclosed requirements are. – R. Martinho Fernandes Aug 22 '12 at 13:59
  • But if I'm going to store an object that is a Pointer returned to me from a different library that I can't or don't want to change - isn't that relevant then? – Deukalion Aug 22 '12 at 14:00
  • @Deukalion pointers are useful in the implementation of container types and in interfacing with C code. There are few other legitimate uses of them. – ecatmur Aug 22 '12 at 14:04
  • 1
    @Deukalion if you get a pointer from another function, are you *sure* you have to delete it? If so, then you are better off storing smart pointers. – juanchopanza Aug 22 '12 at 14:04
  • Changing: vector> to vector> makes T of TestObject go away while trying to access it, changing it back to TestObject* makes it appear and I can store TestObject's. Since I always simplify my code to what I want to do I didn't think of this, but I try to store a pointer as T because it gets returned from another library. – Deukalion Aug 22 '12 at 14:05
  • 1
    @Deukalion if there is a different library involved, that should have been mentioned in your requirements. – ecatmur Aug 22 '12 at 14:06
  • Still, the same layout is required for this to work. I could "require" to use a T as Pointer to object. If this is what I'm wondering, no matter if an external library is included you should still have this is in consideration for the solution. If I said, is this possible and you gave me alternate solutions - there's still no answer to "is it possible". – Deukalion Aug 22 '12 at 14:11
  • 1
    @ecatmur -- you have a syntax error. It should read `pVector.push_back(testObject);`, not `pVector->push_back(testObject);` – Happy Green Kid Naps Aug 22 '12 at 14:30