Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Object cloning

Object cloning

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 1991589
    wrote on last edited by
    #1

    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.

    S _ O 3 Replies Last reply
    0
    • U User 1991589

      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.

      S Offline
      S Offline
      Shmuel Zang
      wrote on last edited by
      #2

      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;
      

      }

      _ 1 Reply Last reply
      0
      • U User 1991589

        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.

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        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)

        Polymorphism in C

        1 Reply Last reply
        0
        • S Shmuel Zang

          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;
          

          }

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          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)

          Polymorphism in C

          1 Reply Last reply
          0
          • U User 1991589

            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.

            O Offline
            O Offline
            Orjan Westin
            wrote on last edited by
            #5

            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 every Person who will be a parent a shared_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 another Person has that grandfather as a parent?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups