Skip to Content
0
Former Member
Feb 24, 2010 at 11:33 PM

"Copy" an object or instance of a class

1321 Views

Ok, so I had this problem.

I was using a class attribute to show some information to the user but also sending this same information to an RFC call.

As RFC calls always end up messing my info in the return, I needed to have two tables with the same info, one for reporting, one for the RFC call.

Instead of just having two tables, and the information was the same, I decided instead to have a class of the same type inside my class. Something like this.

class my_class definition.

private section.
data: i_tab type my_table,
      rfc type ref to my_class.
endclass.

If the user wanted to send the info I would just pass myself to the ref object, if no info was supposed to be sent, I would just create an empty object.

I was wrong, because I thought the rfc would act as a variable, instead, it acts like a pointer, so any change made to rfc->i_tab would affect my_class->i_tab.

I had to choose the two tables solution. But it made me wonder. Is there a way to u201Ccopyu201D an instance of a class? Like in this example.

data: ref1 type ref to my_class,
      ref2 like ref1.

create object ref1.
ref2 = ref1. u201CSome copy operation

I would want ref2 to be an exact copy of the ref1 object but completely independent (not a reference to the ref1 object).