STL Vectors, pointers and classes
Let's say i have 2 classes:
class Class1
{
public:
std::vector<CustomClass3*> mVec;
public:
Class1();
~Class1()
{
//iterate over all the members of the vector and delete the objects
}
};
class InitializerClass2
{
private:
Class1 * mPtrToClass1;
public:
InitializerClass2();
void Initialize()
{
mPtrToClass1->mVec.push_back(new CustomClass3(bla bla parameters));
}
};
Will this work? Or the memory allocated in the Initi开发者_StackOverflow中文版alizerClass2::Initialize() method might get corrupted after the method terminates?
Thanks!
In short this will work fine.
The memory being allocated in Initialize is on the heap. This means that changes in the stack do not affect the contents of this memory.
One issue I see with Class1
is that it is not copy safe yet the copy and assignment constructors have not been suppressed.
This can cause a problem because the destructor of Class1
is noted as freeing the memory for all items in mVec
. Using the implicit operator this means that you'd end up with 2 instances of Class1
pointing to the same CustomClass3
instances and the second destructor would be double deleting memory. For example
Class c1;
c1.mVec.push_back(new CustomClass3(...));
Class c2 = c1;
In this case the second destructor to run (c1) will be freeing an already deleted CustomClass3
instance. You should disable copy construction and assignment for Class1
to prevent this
class Class1 {
...
private:
Class1(const Class1&);
Class1& operator=(const Class1&);
};
It should work (provided mPtrClass1
is a valid pointer of course).
May I suggest that in your InitializerClass2 that you change the constructor to the following:
InitializerClass2() : mPtrToClass1(NULL){}
~InitializerClass2(){
if( mPtrToClass1 != NULL) delete mPtrToClass1;
}
void Initialize(){
if( mPtrToClass1 == NULL){
mPtrToClass1 = new InitializerClass1();
}
mPtrToClass1->mVec.push_back(new CustomClass3(bla bla parameters) );
}
if you're not going to use RAII, so that you don't get issues with checking the destructor.
As to your question, see where I added in the new operator. YOu're not initializing your variable.
精彩评论