Problem to copy objects

Dear Rooters

The attached macro “macroTestCopy.C” shows my problems:

1, Why do methods CopyProjectInfo2() and CopyProjectInfo3() not work?
“.x macroTestCopy.C+” does not print ProjectC and ProjectD, while
“.x macroTestCopy.C” crashes.

2, Why does function TestCopy1() not work?

3, In my real program I need to copy objects, which are created on
the heap (as shown in function TestCopy1()):
What do I need to do to copy an object created on the heap?

Thank you in advance for your help.
Best regards
Christian
macroTestCopy.C (8.64 KB)

Hi Christian,

Your program will work if you fix a bug in your code in
MyClass::CopyProjectInfo3
You are adding to a TList a local object XprojectInfo that is destroyed
when leaving the function. As a result you have a pointer to a dead object
in the TList

Rene

Dear Rene

Thank you, I understand the error, but I have no idea how to solve it.
When I try to create “infocopy” on the heap I get an error.

Furthermore, my real problem is that I would like to use function
"TestCopy1()" which I have commented out since it will not compile.

Best regards
Christian

Hi Christian,

Well! your function TestCopy1 has also several C++ errors.
Change it to:

[code]void TestCopy1()
{
// Test copying of class XProjectInfo

MyClass *myclass = new MyClass(“MyClass”,“test”);

myclass->ProjectInfo(“ProjectA”, 22052005, “descriptionA”);

XProjectInfo *infoB = new XProjectInfo(“ProjectB”, 22052005, “descriptionB”);

myclass->CopyProjectInfo1(*infoB);

XProjectInfo *infoC = new XProjectInfo(“ProjectC”, 22052005, “descriptionC”);

myclass->CopyProjectInfo2(*infoC);

XProjectInfo *infoD = new XProjectInfo(“ProjectD”, 22052005, “descriptionD”);

myclass->CopyProjectInfo3(infoD);

myclass->PrintData(“ProjectA”);
myclass->PrintData(“ProjectC”);
myclass->PrintData(“ProjectB”);
myclass->PrintData(“ProjectB”);

delete myclass;
delete infoB;
delete infoC;
delete infoD;
}[/code]

Rene

Hi Christian,

Why do you have

XProjectInfo *infocopy = new XProjectInfo(); infocopy->Copy(info); fList->Add(infocopy);
but you also have

// XProjectInfo *infocopy = new XProjectInfo(); XProjectInfo infocopy; info->Copy(infocopy); fList->Add(&infocopy);
I think that the second should be

XProjectInfo *infocopy = new XProjectInfo(); infocopy->Copy(info); fList->Add(&infocopy); I.e. fix both the memory error and the copy direction error.
Also this can not work in interpreted mode since you class derived from a compiled class AND overload a virtual function.

Cheers,
Philippe.

Dear Rene, dear Philippe

Thank you, I did not know that I can write Function(*var), i.e.
myclass->CopyProjectInfo1(*infoB);

The code of CopyProjectInfo3(XProjectInfo *info) must be:

   XProjectInfo *infocopy = new XProjectInfo();
   info->Copy(*infocopy);
   fList->Add(infocopy);

Additionally, I could also use:

   XProjectInfo *infocopy = (XProjectInfo*)info->Clone();
   fList->Add(infocopy);

Are there any rules, when to use obj->Copy() and when to use obj->Clone()?
Which one should normally be used?

BTW, now my macro works also in interpreted mode :slight_smile:

Best regards
Christian