How to duplicate an object?
-
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 -
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.comSomething 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
-
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.comIn 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.
-
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.
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 havechar
arrays,string
s 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 -
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 havechar
arrays,string
s 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