Reputation: 317
I have class scene (from assimp library) and I extended it with some functions and made class sceneChild. Now I need to copy data from instance of scene into sceneChild instance. I know it is possible to write manually something like this: sceneChild.a=scene.a , sceneChild.b=scene.b ... or this.a=scene.a , this.b=scene.b in copy constructor. However something tells me that this can be done in few lines of code. Am I right? If so then how this can be done? Thank you!
I ask about it because I need to perform this copy operation not only with scene class but with many other classes that are full of data and therefore it will take too long to do it manually.
Example:
class Parent
{
public:
string name;
};
class Child: Parent
{
public:
int age;
};
int main()
{
Parent p;
p.name = "some name";
Child c(p); // some magic here so that data (in this case string "name")
//is copied from p to c so that c.name=="some name"
return 0;
}
Upvotes: 2
Views: 633
Reputation: 8464
I'm not famliar with the scene class, but generaly you can use initialization list in the constructor to initialize the parent class.
In your example you can use:
class Parent
{
public:
string name;
};
class Child: public Parent
{
public:
int age;
Child(Parent& p) : Parent(p) {}
};
Upvotes: 1