Results 1 to 7 of 7

Thread: Help on set and get a object copy

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Help on set and get a object copy

    I dont know if this can be done.-
    I have a method to set a copy of an object (with some structs instances an vectors inside it)

    Inside the class I have to do the work, I have a private pointer based object_undo:
    Qt Code:
    1. ClassX * the_object_undo;
    2.  
    3. void w_set_undo(CLass the_object)
    4. { the_object_undo = &the_object }
    5.  
    6. ClassX * w_get_undo(){ return the_object_undo}
    To copy to clipboard, switch view to plain text mode 

    I have a original_object pointer based in another class.
    I call
    Qt Code:
    1. w_set_undo(*original_object);
    To copy to clipboard, switch view to plain text mode 

    Ok, Inside w_set_undo, if I check some properties of 'the_object_undo' they are correct. However, inside w_get_undo, they are not ( so I cant to retunr a wrong object). What is happen ? A solution ?

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help on set and get a object copy

    The code is incorrect. The solution is to pass a pointer or store the object.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help on set and get a object copy

    I want to store the object.
    The object is pointer based, so to pass to the function i have to write *object, isn't it ?
    And.... the function set_undo_object works
    (I can play with the object received as copy )

    Please help....

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help on set and get a object copy

    Quote Originally Posted by tonnot View Post
    I want to store the object.
    The object is pointer based, so to pass to the function i have to write *object, isn't it ?
    The two sentences you have written above are contradictory. Think what you really want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help on set and get a object copy

    Solved
    Inside set_undo I must to have :
    Qt Code:
    1. void w_set_undo(CLassX the_object)
    2. {
    3. object_undo = new ClassX() ; (the object must exist before)
    4. *object_undo = the_object }
    To copy to clipboard, switch view to plain text mode 

    It seems that works !
    What do you think ?
    Thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help on set and get a object copy

    It makes little sense to do that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Help on set and get a object copy

    As wysota said, you code makes no functional sense, and unless you are taking care of your 'object_undo' pointer (which doesn't look like you do), you are leaking memory as well.
    If what you want is an "undo" functionality, Qt has a built in framework for that:
    http://doc.qt.nokia.com/latest/qundo.html
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 4
    Last Post: 15th July 2011, 18:31
  2. Copy an Object
    By Qiieha in forum Qt Programming
    Replies: 2
    Last Post: 11th July 2011, 08:15
  3. Replies: 4
    Last Post: 20th August 2010, 13:07
  4. Replies: 1
    Last Post: 30th July 2010, 23:05
  5. writing object to the file(Object Persistance)
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 11th June 2009, 14:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.