class Base
{
public:
Base (Mesh _mesh, Material _material) :
mesh (_mesh),
material (_material),
{}
private:
Mesh mesh;
Material material;
}
Now, I have a class that derives from this class
class B : public Base
{
public:
B () :
Base (Mesh (arg1, arg2, arg3),
Material (arg1, arg2, arg3))
...
{}
}
As you can see, I pass arguments to the base constructor by calling constructors for its arguments. However, the mesh class has a default copy operator, and to avoid a double-free in this situation will need to copy all the memory to another place (ie. override this default copy operator), because it has dynamic memory. It will get created in the scope of B, the passed to Base, copied, and then when it returns the original will be deleted, and in this case will suffer a double-free error if I don't override the copy constructor. It contains alot of data, so I don't want to be copying it. Material is in no such case. (It can be copied fine without a double-free error) Rather than just base the mesh arguments to the base class and have it instantiate these objects directly, is there a better way to do this? I could use pointers but I would have to delete the pointers in the deconstructor of Base, which would go wrong if I it was based a non 'newed' pointer, for example a pointer to data on the stack. Thanks! In Java you'd just use that syntax (not that I'm THAT familiar with java) maybe some insight into how it handles its garbage collection in this case? Or does java just create two copies like I said was happening?