Object cloning
-
Dear all, Consider the following code:
class Person
{
Person *parent;public:
Person(Person *p){parent = p;}};
int main()
{
Person grandfather(nullptr);
Person father(&grandfather);
Person son(&father);// Do something here return 0;
}
The inter-object relationship works fine, as long as I don't pass the objects around as function arguments (e.g. pass by value) or clone the objects. If I want to pass them as function parameters, or make a clone of the three objects, without breaking their relationships, what should I do? Thanks. Milan.
-
Dear all, Consider the following code:
class Person
{
Person *parent;public:
Person(Person *p){parent = p;}};
int main()
{
Person grandfather(nullptr);
Person father(&grandfather);
Person son(&father);// Do something here return 0;
}
The inter-object relationship works fine, as long as I don't pass the objects around as function arguments (e.g. pass by value) or clone the objects. If I want to pass them as function parameters, or make a clone of the three objects, without breaking their relationships, what should I do? Thanks. Milan.
Maybe a std::shared_ptr can be helpful for you.
You can use it as follows:
#include <memory>
using namespace std;
class Person
{
shared_ptr<Person> parent;public:
Person(shared_ptr<Person> p) {parent = p;}};
void DoSomething(shared_ptr<Person> p)
{
// Do something here
}int main()
{
shared_ptr<Person> grandfather = make_shared<Person>(nullptr);
shared_ptr<Person> father = make_shared<Person>(grandfather);
shared_ptr<Person> son = make_shared<Person>(father);DoSomething(father); return 0;
}
-
Dear all, Consider the following code:
class Person
{
Person *parent;public:
Person(Person *p){parent = p;}};
int main()
{
Person grandfather(nullptr);
Person father(&grandfather);
Person son(&father);// Do something here return 0;
}
The inter-object relationship works fine, as long as I don't pass the objects around as function arguments (e.g. pass by value) or clone the objects. If I want to pass them as function parameters, or make a clone of the three objects, without breaking their relationships, what should I do? Thanks. Milan.
Not sure what exactly is not working for you, but if object lifetime is the problem, you could use
shared_ptr
.class Person
{
shared_ptr<Person> parent;public:
Person(shared_ptr<Person> p){ parent = p; }};
int main()
{
auto grandfather(make_shared<Person>(nullptr));
auto father(make_shared<Person>(grandfather));
auto son(make_shared<Person>(father));// Do something here return 0;
}
«_Superman_» _I love work. It gives me something to do between weekends.
_Microsoft MVP (Visual C++) (October 2009 - September 2013)
-
Maybe a std::shared_ptr can be helpful for you.
You can use it as follows:
#include <memory>
using namespace std;
class Person
{
shared_ptr<Person> parent;public:
Person(shared_ptr<Person> p) {parent = p;}};
void DoSomething(shared_ptr<Person> p)
{
// Do something here
}int main()
{
shared_ptr<Person> grandfather = make_shared<Person>(nullptr);
shared_ptr<Person> father = make_shared<Person>(grandfather);
shared_ptr<Person> son = make_shared<Person>(father);DoSomething(father); return 0;
}
You just beat me to it. :)
«_Superman_» _I love work. It gives me something to do between weekends.
_Microsoft MVP (Visual C++) (October 2009 - September 2013)
-
Dear all, Consider the following code:
class Person
{
Person *parent;public:
Person(Person *p){parent = p;}};
int main()
{
Person grandfather(nullptr);
Person father(&grandfather);
Person son(&father);// Do something here return 0;
}
The inter-object relationship works fine, as long as I don't pass the objects around as function arguments (e.g. pass by value) or clone the objects. If I want to pass them as function parameters, or make a clone of the three objects, without breaking their relationships, what should I do? Thanks. Milan.
If you want to use them as function parameters, pass them by reference, not by value.
void function1(Person p)
{
// p is a copy
}void function2(Person& p)
{
// p is a reference to the object passed
// in as a parameter, not a copy
}If you use
shared_ptr
, remember that you have to make everyPerson
who will be a parent ashared_ptr
, otherwise its child will try to delete it (which it has no right to do) when the last goes out of scope. If you want to create a clone of a Person, and clone its relations, you will need to think hard about lifetime management and ownership. What should happen if you tried to clone father? Should it create a clone of grandfather? That is quite simple to do, but it means father will have to delete its grandfather clone in its destructor. Then what happens if anotherPerson
has that grandfather as a parent?