Yes that should just copy the pointer, after that the layer in the vector is deleted so the pointer should only exist in the copied layer at that point. I set the pointers to NULL after freeing in the destructor but somehow when layers are created again a pointer is created to the same memory.
There is no special implementation of copy or assignment.
It's kind of hard to follow but I think you should google "The rule of three".
Here's a quick example of what will happen if you violate it.
Code:
#include <iostream>
#include <vector>
struct CopyingBroken
{
int* p;
CopyingBroken(int value): p(new int(value)) {}
~CopyingBroken()
{
std::cout << p << " destructor\n";
delete p;
p = 0;
}
//if you don't implement anything better, this is what the compiler provides for you
//the pointer is shared but you have no idea how many other instances might be there,
//all pointing to the same int
CopyingBroken(const CopyingBroken& other): p(other.p)
{}
const CopyingBroken& operator= (const CopyingBroken& other)
{
p = other.p; //leaks memory held by p, and makes this share the pointer with other
return *this;
}
};
int main()
{
std::vector<CopyingBroken> vec;
CopyingBroken broken(1);
vec.push_back(broken);
}
My output (of course the program could do anything since the result of double deletion is undefined):
0x3e2450 destructor
0x3e2450 destructor
One of the objects is called broken, and a copy is stored in the vector. When they go out of scope, they each deallocate the same memory. Setting a member's value to NULL in the destructor has naturally no effect to any other object, similarly as you won't expect that effect from this simple code:
Code:
int a = 10;
int b = a;
a = 0;
//what is the value of b?