Reputation: 135
I have a class, GameObject, which has a std::vector<Component*> mComponents
and I have overloaded the GameObject(const GameObject&)
. I am trying to copy the mComponents over from one to the other, but making each of the contained Component*
's into a new
object entirely, but keeping the objects contents exactly the same. This is what I have at the moment:
GameObject.cpp
GameObject::GameObject(const GameObject& other)
{
if (this != &other)
{
this->mComponents = other.mComponents; // EDIT 1
for (int i = 0; i < (int)mComponents.size(); i++)
{
// FILL this->mComponents with NEW objects but
// containing the exact same data and
// variables from other.mComponents
this->mComponents[i] = other.Copy(); // EDIT 2 EXAMPLE OF IDEA IN COMMENTS
this->mComponents[i]->setParent(this);
}
}
}
Engine.cpp (Extract)
GameObject cube, cube2;
cube.addComponent(new DirectionalLight(glm::vec3(-0.2f, -1.0f, -0.3f)));
cube.addComponent(new Texture("Resources/Textures/container.png", "Resources/Textures/container_specular.png"));
cube.addComponent(new Shader("Resources/Shaders/cube.shader"));
cube.addComponent(new Cube());
cube2 = GameObject(cube);
When I instantiate cube2, the mComponents Components*
contents all stay exactly the same but I would like to create a new Component*
's to fill this std::vector
from the GameObject(const GameObject&)
fucntion, whilst keeping all the variables the same.
P.s. I know that most other operators such as '=' will not create new Components for inside the vector but I will be implementing that aswell after I figure out how to fill the vector with new Component*
's.
Upvotes: 0
Views: 89
Reputation: 1746
this->mComponents[i]->Copy(other);
Will not work. At least, not from a pure inheritance standpoint. A supertype (base) type pointer cannot be implicitly cast to a derived type. This is called downcasting, and no language supports it implicitly.
A simpler way to do it is to define a virtual "clone" function in each component:
virtual Component* clone()=0; // base declaration
virtual Component* Texture::clone() //derived implementation
{
return new Texture(*this);
}
Then in your game object copy constructor:
for (int i = 0; i < (int)other.mComponents.size(); i++)
{
// FILL this->mComponents with NEW objects but
// containing the exact same data and
// variables from other.mComponents
this->mComponents.push_back(other.mComponents->clone());
this->mComponents[i]->setParent(this);
}
This way, you let the component itself handle the copying process.
Upvotes: 4