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. How to duplicate an object?

How to duplicate an object?

Scheduled Pinned Locked Moved C / C++ / MFC
comtutorialquestion
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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi! I want to duplicate an object previously instanced: CMyClass* pMyVar1=new CMyClass (); pMyVar1->Var="Value"; CMyClass* pMyVar2=new CMyClass (); CopyMemory (pMyVar2, pMyVar1, sizeof(pMyVar1)); This code doesn't work, because I think that the sizeof is not correct. Is there a way to copy an object with all his values? Thks in advance! Appstmd http://www.appstmd.com

    J T 2 Replies Last reply
    0
    • L Lost User

      Hi! I want to duplicate an object previously instanced: CMyClass* pMyVar1=new CMyClass (); pMyVar1->Var="Value"; CMyClass* pMyVar2=new CMyClass (); CopyMemory (pMyVar2, pMyVar1, sizeof(pMyVar1)); This code doesn't work, because I think that the sizeof is not correct. Is there a way to copy an object with all his values? Thks in advance! Appstmd http://www.appstmd.com

      J Offline
      J Offline
      Jeryth
      wrote on last edited by
      #2

      Something along the lines of

      *pMyVar2 = *pMyVar1;

      I just made a simple program that does the same thing and it works.

      #include
      using namespace std;

      class Class1
      {
      public:
      int var1;
      int var2;
      };

      int main()
      {
      Class1 * object1 = new Class1();
      Class1 * object2 = new Class1();

      object1->var1 = 1;
      object1->var2 = 3;
      
      object2->var1 = 2;
      object2->var2 = 4;
      
      cout << "Object1 Var1 = " << object1->var1 << endl;
      cout << "Object1 Var2 = " << object1->var2 << endl;
      cout << "Object2 Var1 = " << object2->var1 << endl;
      cout << "Object2 Var2 = " << object2->var2 << endl << endl;
      
      \*object2 = \*object1;
      cout << "Object2 Var1 = " << object2->var1 << endl;
      cout << "Object2 Var2 = " << object2->var2 << endl;
      
      delete object1;
      object1 = NULL;
      delete object2;
      object2 = NULL;
      
      system( "pause" );
      return 0;
      

      }

      object2 was switched in the output so I don't know why it wouldn't work for a more complicated class. The question "Do computers think?" is the same as "Can submarines swim?" DragonFire Software Jeryth

      1 Reply Last reply
      0
      • L Lost User

        Hi! I want to duplicate an object previously instanced: CMyClass* pMyVar1=new CMyClass (); pMyVar1->Var="Value"; CMyClass* pMyVar2=new CMyClass (); CopyMemory (pMyVar2, pMyVar1, sizeof(pMyVar1)); This code doesn't work, because I think that the sizeof is not correct. Is there a way to copy an object with all his values? Thks in advance! Appstmd http://www.appstmd.com

        T Offline
        T Offline
        Tim Smith
        wrote on last edited by
        #3

        In theory what you did "could" work but in practice it is breaking so many rules that you should never do it. In your case, "*pMyVar2 = *pMyVar1" should work, however, that assumes that if a default assignment operator isn't defined, that the default one (of just copying the elements blindly) would work. Many times it will not for an assortment of reasons. In those cases, you have to define your own assignment operator to handle the operation properly. IMHO, the default assignment operator is more trouble than it is worth. Many people will thwart the default assignment operator if you shouldn't be performing that operation on the class. If your class can be used with an assignment operator, go to the trouble of defining one. Tim Smith I'm going to patent thought. I have yet to see any prior art.

        J 1 Reply Last reply
        0
        • T Tim Smith

          In theory what you did "could" work but in practice it is breaking so many rules that you should never do it. In your case, "*pMyVar2 = *pMyVar1" should work, however, that assumes that if a default assignment operator isn't defined, that the default one (of just copying the elements blindly) would work. Many times it will not for an assortment of reasons. In those cases, you have to define your own assignment operator to handle the operation properly. IMHO, the default assignment operator is more trouble than it is worth. Many people will thwart the default assignment operator if you shouldn't be performing that operation on the class. If your class can be used with an assignment operator, go to the trouble of defining one. Tim Smith I'm going to patent thought. I have yet to see any prior art.

          J Offline
          J Offline
          Jeryth
          wrote on last edited by
          #4

          Very true, I hadn't thought of default = difficulties originally. If your class is just a bunch of int, double, etc the default = operator should work. But if you have char arrays, strings and other more complicated vars, definitely define your own = operator. The question "Do computers think?" is the same as "Can submarines swim?" DragonFire Software Jeryth

          A 1 Reply Last reply
          0
          • J Jeryth

            Very true, I hadn't thought of default = difficulties originally. If your class is just a bunch of int, double, etc the default = operator should work. But if you have char arrays, strings and other more complicated vars, definitely define your own = operator. The question "Do computers think?" is the same as "Can submarines swim?" DragonFire Software Jeryth

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            Thks! I've defined my own = operator for my class and it works now.

            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