Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I copy an Object (Object Pointer) to another?
Forum Updated to NodeBB v4.3 + New Features

How do I copy an Object (Object Pointer) to another?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 11.5k 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.
  • D Offline
    D Offline
    dcbasso
    wrote on 12 Dec 2012, 11:47 last edited by
    #1

    I have an object and I want to make a copy of them.
    The that that I only have the pointer of this object, as see below:

    @
    MyObject* myObj = new MyObject();
    @

    I need to make a copy of this object, I need to have two objects but in different pointer reference... I don't need to link one to another, I need a copy.
    How can I make that?

    Thanks

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on 12 Dec 2012, 12:29 last edited by
      #2

      -deleted-

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tobias.hunger
        wrote on 12 Dec 2012, 14:26 last edited by
        #3

        You can not copy QObjects. See https://qt-project.org/doc/qt-4.8/object.html#identity-vs-value .

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MuldeR
          wrote on 12 Dec 2012, 22:58 last edited by
          #4

          In C++ you would create a copy of an object by using the copy constructor.

          With local objects:
          @MyObject obj1;
          MyObject obj2(obj1);@

          With pointers to objects:
          @MyObject *obj1 = new MyObject();
          MyObject *obj2 = new MyObject(*obj1);@

          Note that if your class "MyObject" has pointer member variables, you may need to implement your own copy constructor (instead of the implicit one) in order to ensure the desired "deep copy" behavior.

          See also:
          http://www.cplusplus.com/articles/y8hv0pDG/

          But as already mentioned:
          [quote]QObject and all subclasses of QObject (direct or indirect) have their copy constructor and assignment operator disabled![/quote]

          My OpenSource software at: http://muldersoft.com/

          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

          Go visit the coop: http://youtu.be/Jay...

          1 Reply Last reply
          0

          1/4

          12 Dec 2012, 11:47

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved